代码之家  ›  专栏  ›  技术社区  ›  drewpol

通过原子bool崩溃完成线程

  •  0
  • drewpol  · 技术社区  · 7 年前

    我有以下工作线程:

    class CWorkerThread
    {
    public:
        CWorkerThread(){}
        ~CWorkerThread(){}
    
        bool startUiThread()
        {
            try
            {
                std::thread th(&CRefreshUiWorkerThread::operator(), this);
                if (th.joinable())
                {
                    th.detach();
                }
            }
            catch (std::exception ex)
            {
                //print logs
                return false;
            }
    
            //print logs
            return true;
        }
    
        void stopUiThread()
        {
            m_bFinishFlag = true;
        }
    
    protected:
        void operator()()
        {
            while (!refFinishFlag)
            {
                //Do some stuff here
    
                //Sleep thread
                std::this_thread::sleep_for(std::chrono::seconds(6));
            }
        }
    
    private:
        std::atomic<bool> m_bFinishFlag = false;
    };
    

    这是mfc dll的一部分。在完成dll期间,我想使用原子bool标志终止这个线程。所以在MFC里面 ExitInstance ,方法 stopUiThread 被称为。但我有以下错误:

    First-chance exception at 0x08789E64 (mydll.dll) in MyApp.exe: 0xC0000005: Access violation writing location 0x00000030.
    

    这是发生错误的调用堆栈:

    >   mydll.dll!std::_Store_seq_cst_1(volatile unsigned char * _Tgt, unsigned char _Value) Line 343   C++
        mydll.dll!std::_Atomic_store_1(volatile unsigned char * _Tgt, unsigned char _Value, std::memory_order _Order) Line 361  C++
        mydll.dll!std::atomic_store_explicit(std::atomic_bool * _Atom, bool _Value, std::memory_order _Order) Line 546  C++
        mydll.dll!std::atomic_store(std::atomic_bool * _Atom, bool _Value) Line 558 C++
        mydll.dll!std::atomic_bool::operator=(bool _Value) Line 756 C++
        mydll.dll!std::atomic<bool>::operator=(bool _Val) Line 204  C++
        mydll.dll!CWorkerThread::stopUiThread() Line 87 C++
        mydll.dll!CMyDll::ExitInstance() Line 97    C++
    

    有没有人告诉我这个错误是从哪里来的,怎么解决?

    0 回复  |  直到 7 年前