VC中的std::thread类++ 11 导致随机崩溃。有什么解决方法吗?
我在 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 库并不重要(它们都是多线程的)..
错误报告
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
堆栈跟踪(访问冲突):
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
谢谢!
I've encounter a bug in Visual Studio 11 Developer Preview, at least I think it is a bug and reported it, but I'm interested whether someone know a workaround.
When I use std::thread
class to create more then one thread it causes application to crash. Sometimes it throws exception, sometimes it causes access violation and sometimes it works. Code that reproduces the bug looks like this:
#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;
}
It doesn't matter whether the static or dynamic CRT libraries are used (all of them are multi-threaded)..
bug report
Stacktrace (thrown exception, unlocking unowned mutex):
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 (access violation):
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
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个已知问题。请参阅以下连接错误:
在该错误报告的评论中,Stephan 说,“我们已经修复了它,并且该修复将在 VC11 中提供。” (我不知道该修复是否会出现在 VC11 Beta 中。我们将在下周知道。)
This is a known issue. See the following Connect bug:
In the comments of that bug report, Stephan says, "We've fixed it, and the fix will be available in VC11." (Whether the fix will be present in the VC11 Beta, I do not know. We'll find out next week.)
我在 VS11 开发预览中没有遇到任何线程问题。以下内容对我有用。我必须使用稍微不同的构建设置,因为我必须删除对
_TCHAR
的引用。如果这不是开发预览版以来的新错误,那么调整构建设置可能会有所帮助。我使用的临时项目大部分应该是默认的。我记得的唯一更改是手动禁用和删除预编译头内容,因为取消选中项目创建向导中的复选框永远不会执行任何操作。而且你也不需要指针。您可以使用
std::vector
(或std::array
),因为std::thread
是可移动的。I haven't had any problems with threads in the VS11 dev preview. The following works for me. I must be using slightly different build settings because I had to remove the references to
_TCHAR
. If it's not a new bug since the dev preview then maybe messing around with the build settings will help. The scratch project I'm using should mostly be the default. The only thing I remember change was manually disabling and deleting the precompiled header stuff, because unchecking that check box in the project creation wizard never does anything.Also you don't need pointers. You can use
std::vector<std::thread>
(orstd::array
) becausestd::thread
is movable.