VC ++ 11中的std :: thread类导致随机崩溃。任何解决方法?

时间:2012-02-23 21:02:07

标签: c++ multithreading visual-c++ stl visual-c++-2012

我在Visual Studio 11开发者预览版中遇到了一个错误,至少我认为这是一个错误并报告了它,但我对是否有人知道解决方法感兴趣。

当我使用std::thread类创建多个线程时,它会导致应用程序崩溃。有时它抛出异常,有时会导致访问冲突,有时它会起作用。重现错误的代码如下所示:

#include <iostream>
#include <thread>
#include <vector>

#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
 std::vector<std::thread*> threads;
 for(int i = 0; i < 10; i++)
 {
   threads.push_back(new std::thread([i]
     {
       /*std::cout << "thread " << i << std::endl;*/
       /* whatever else that is thread safe, or even an empty lambda */
     }));
 }

 for(int i = 0; i < 10; i++)
 {
   threads[i]->join();
   delete threads[i];
 }

 return 0;
}

是否使用静态或动态CRT库(所有这些都是多线程的)无关紧要。

bug report

Stacktrace(抛出异常,解锁无主互斥锁):

test.exe!_NMSG_WRITE(int rterrnum) Line 217 C
test.exe!abort() Line 62    C
test.exe!_Thrd_abort(const char * msg) Line 111 C
test.exe!_Mtx_unlock(_Mtx_internal_imp_t * * mtx) Line 206  C++
test.exe!_Save_state(_Mtx_internal_imp_t * * mtx, _Mtx_state * state) Line 266  C++
test.exe!do_wait(_Cnd_internal_imp_t * * cond, _Mtx_internal_imp_t * * mtx, const xtime * target) Line 103  C
test.exe!_Cnd_wait(_Cnd_internal_imp_t * * cond, _Mtx_internal_imp_t * * mtx) Line 198  C
test.exe!std::_Cnd_waitX(_Cnd_internal_imp_t * * _Cnd, _Mtx_internal_imp_t * * _Mtx) Line 94    C++
test.exe!std::_Pad::_Launch(_Thrd_imp_t * _Thr) Line 97 C++
test.exe!??$_Launch@V?$_Bind@$0A@XV<lambda_1B7F0477D0C0EDFD>@?4?wmain@U_Nil@std@@U23@U23@U23@U23@U23@U23@@std@@@std@@YAXPAU_Thrd_imp_t@@ABV?$_Bind@$0A@XV<lambda_1B7F0477D0C0EDFD>@?4?wmain@U_Nil@std@@U23@U23@U23@U23@U23@U23@@0@@Z(_Thrd_imp_t * _Thr, const std::?$_Bind@$0A@XV<lambda_1B7F0477D0C0EDFD>@?4?wmain@U_Nil@std@@U23@U23@U23@U23@U23@U23@ & _Tg) Line 207    C++
test.exe!main::main(wmain::__l5::<lambda_1B7F0477D0C0EDFD> _Fx) Line 47 C++
test.exe!wmain(int argc, wchar_t * * argv) Line 17  C++
test.exe!__tmainCRTStartup() Line 238   C
test.exe!wmainCRTStartup() Line 168 C
kernel32.dll!76d7339a() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
ntdll.dll!77cc9ef2()    Unknown
ntdll.dll!77cc9ec5()    Unknown

Stacktrace(访问冲突):

test.exe!_Mtx_unlock(_Mtx_internal_imp_t * * mtx) Line 218  C++
test.exe!std::_Mtx_unlockX(_Mtx_internal_imp_t * * _Mtx) Line 84    C++
test.exe!std::_Pad::_Release() Line 105 C++
test.exe!?_Run@?$_LaunchPad@V?$_Bind@$0A@XV<lambda_1B7F0477D0C0EDFD>@?4?wmain@U_Nil@std@@U23@U23@U23@U23@U23@U23@@std@@@std@@CAIPAV12@@Z(std::?$_LaunchPad@V?$_Bind@$0A@XV<lambda_1B7F0477D0C0EDFD>@?4?wmain@U_Nil@std@@U23@U23@U23@U23@U23@U23@@std@@ * _Ln) Line 195  C++
test.exe!?_Go@?$_LaunchPad@V?$_Bind@$0A@XV<lambda_1B7F0477D0C0EDFD>@?4?wmain@U_Nil@std@@U23@U23@U23@U23@U23@U23@@std@@@std@@UAEIXZ() Line 187   C++
test.exe!_Call_func(void * _Data) Line 52   C++
test.exe!_callthreadstartex() Line 308  C
test.exe!_threadstartex(void * ptd) Line 291    C
kernel32.dll!76d7339a() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
ntdll.dll!77cc9ef2()    Unknown
ntdll.dll!77cc9ec5()    Unknown

谢谢!

2 个答案:

答案 0 :(得分:6)

这是一个已知问题。请参阅以下Connect错误:

  

std::thread crashes with error "f:\dd\vctools\crt_bld\self_x86\crt\src\thr\mutex.cpp(206): unlock of unowned mutex"

在该错误报告的评论中,Stephan说,“我们已经修复了它,修复程序将在VC11中提供。” (修补程序是否会出现在VC11测试版中,我不知道。下周我们会发现。)

答案 1 :(得分:0)

VS11 dev预览中的线程没有任何问题。以下适用于我。我必须使用略有不同的构建设置,因为我必须删除对_TCHAR的引用。如果它不是一个新的错误,因为开发预览然后可能搞乱编译设置将有所帮助。我正在使用的临时项目应该是默认的。我唯一记得改变的是手动禁用和删除预编译的头文件,因为在项目创建向导中取消选中该复选框永远不会做任何事情。

#include <iostream>
#include <thread>
#include <vector>

#include <Windows.h>

int main(int argc, char* argv[])
{
 std::vector<std::thread*> threads;
 for(int i = 0; i < 10; i++)
 {
   threads.push_back(new std::thread([i]
     {
       std::cout << "thread " << i << std::endl;
       /* whatever else that is thread safe, or even an empty lambda */
     }));
 }

 for(int i = 0; i < 10; i++)
 {
   threads[i]->join();
   delete threads[i];
 }

 return 0;
}

此外,您不需要指针。您可以使用std::vector<std::thread>(或std::array),因为std::thread可以移动。

std::array<std::thread,10> threads;
for(int i = 0; i < threads.size(); i++)
    threads[i] = std::thread([i] { std::cout << "thread " << i << std::endl; });

for(int i = 0; i < 10; i++)
    threads[i].join();