c – std :: thread在使用参数创建时抛出访问冲突异常?
我正在使用VS2015并在使用std :: thread时遇到一个非常奇怪的问题.
void Klass::myfunc(int a,int b) { std::cout << a << ' ' << b << std::endl; } // ... auto t = std::thread(&Klass::myfunc,this,100,200); <- runtime error after called // ... t.join(); 它在调试模式下运行良好,但在转向发布模式时会抛出“访问冲突异常”. 更重要的是,如果我尝试将“myfunc”修改为: void Klass::myfunc() { std::cout << "foo" << std::endl; } // ... auto t = std::thread(&Klass::myfunc,this); // everything goes well // ... t.join(); 它再次运作良好. 我保证“& Klass :: myfunc”和“this”指针不是NULL.当调用ctor时,在几行之后有一个“连接”. 我想这可能是某种“未定义的行为”,但我不知道究竟是什么. 调用堆栈是这样的: 000000c83a4ffd40() Unknown > distributed_word_embedding.exe!std::_LaunchPad<std::unique_ptr<std::tuple<void (__cdecl multiverso::Communicator::*)(void) __ptr64,multiverso::Communicator * __ptr64>,std::default_delete<std::tuple<void (__cdecl multiverso::Communicator::*)(void) __ptr64,multiverso::Communicator * __ptr64> > > >::_Run(std::_LaunchPad<std::unique_ptr<std::tuple<void (__cdecl multiverso::Communicator::*)(void),multiverso::Communicator *>,std::default_delete<std::tuple<void (__cdecl multiverso::Communicator::*)(void),multiverso::Communicator *> > > > * _Ln) Line 247 C++ distributed_word_embedding.exe!std::_Pad::_Call_func(void * _Data) Line 210 C++ ucrtbase.dll!00007ffabdc7be1d() Unknown kernel32.dll!00007ffabfae8102() Unknown ntdll.dll!00007ffac26bc5b4() Unknown 解决方法
你应该总是确保你加入(或可能分离)一个线程,否则主要特别是使用对象的线程(在这种情况下这个)会(有时)导致问题.
//... details omitted... int main() { auto t = std::thread(&Klass::myfunc,this); t.join(); //<----- NOTE THIS } 安东尼威廉的线程blog详细介绍了这一点.举个例子与你的第二个非常类似: void my_thread_func() { std::cout<<"hello"<<std::endl; } int main() { std::thread t(my_thread_func); } 他说
然后他介绍了如上所述使用join的想法,并说,
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |