c++ - PPL critical_section/parallel_for 在 Visual Studio 2010 下崩溃?

标签 c++ visual-studio-2010 visual-c++ c++11 ppl

我制作了这个简单的测试程序,它有时会触发调试中断,或者只是崩溃(在 Debug/Win32/VS2010SP1 下) - 当然,有时它甚至可以工作。我做错了什么或者 PPL (VS2010) 中的某个地方有错误吗?

#include "stdafx.h"
#include <ppl.h>
#include <vector>

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<int> vi;
    Concurrency::critical_section cs;
    Concurrency::parallel_for(0, 10000, [&](int i)
    {
        Concurrency::critical_section::scoped_lock l(cs);
        vi.push_back(i);
    });
    return 0;
}

调试中断调用堆栈如下所示:

msvcr100d.dll!_CrtDbgBreak() Line 85 C msvcr100d.dll!_VCrtDbgReportW(int nRptType=2, const wchar_t * szFile=0x0f45d230, int nLine=728, const wchar_t * szModule=0x00000000, const wchar_t * szFormat=0x0f45d400, char * arglist=0x7d92f7c4) Line 502 C msvcr100d.dll!_CrtDbgReportWV(int nRptType=2, const wchar_t * szFile=0x0f45d230, int nLine=728, const wchar_t * szModule=0x00000000, const wchar_t * szFormat=0x0f45d400, char * arglist=0x7d92f7c4) Line 241 + 0x1d bytes C++ msvcr100d.dll!_CrtDbgReportW(int nRptType=2, const wchar_t * szFile=0x0f45d230, int nLine=728, const wchar_t * szModule=0x00000000, const wchar_t * szFormat=0x0f45d400, ...) Line 258 + 0x1d bytes C++ msvcr100d.dll!Concurrency::details::LockQueueNode::Copy(Concurrency::details::LockQueueNode * pCopyFromNode=0x7d92f908) Line 728 + 0x27 bytes C++ msvcr100d.dll!Concurrency::critical_section::_Acquire_lock(void * _PLockingNode=0x7d92f908, bool _FHasExternalNode=true) Line 1019 C++ msvcr100d.dll!Concurrency::critical_section::scoped_lock::scoped_lock(Concurrency::critical_section & _Critical_section=locked) Line 1083 C++ Lockable.exe!anonymous namespace'::<lambda0>::operator()(int i=1418) Line 14 + 0x11 bytes C++ Lockable.exe!Concurrency::_Parallel_chunk_helper_invoke<int,unsigned int,anonymous namespace'::,0>::_Invoke(const int & _First=0, unsigned int & _Index=1418, const anonymous-namespace'::<lambda0> & _Func={...}) Line 1445 C++ Lockable.exe!Concurrency::_Parallel_chunk_helper<int,unsigned int,anonymous namespace'::,0>::operator()() Line 1781 + 0x16 bytes C++ Lockable.exe!Concurrency::task_handle,0> > >(Concurrency::task_handle,0> > * _PChore=0x7c13fba8 {_M_first=0 _M_step=1 _M_function={...} ...}) Line 3495 C++ msvcr100d.dll!Concurrency::details::_UnrealizedChore::_StructuredChoreWrapper(Concurrency::details::_UnrealizedChore * pChore=0x7c13fba8 {_M_first=0 _M_step=1 _M_function={...} ...}) Line 99 + 0xc bytes C++ msvcr100d.dll!Concurrency::details::_UnrealizedChore::_Invoke() Line 3454 + 0xc bytes C++ msvcr100d.dll!Concurrency::details::WorkItem::Invoke() Line 75 C++ msvcr100d.dll!Concurrency::details::InternalContextBase::ExecuteChoreInline(Concurrency::details::WorkItem * pWork=0x7d92fe7c) Line 1385 C++ msvcr100d.dll!Concurrency::details::InternalContextBase::Dispatch(Concurrency::DispatchState * pDispatchState=0x7d92fe9c) Line 1478 C++ msvcr100d.dll!Concurrency::details::FreeThreadProxy::Dispatch() Line 157 C++ msvcr100d.dll!Concurrency::details::ThreadProxy::ThreadProxyMain(void * lpParameter=0x2ed5b4f0) Line 162 C++ kernel32.dll!763c33aa()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!771a9ef2()
ntdll.dll!771a9ec5()

我刚刚遇到的一次崩溃看起来锁尚未被持有(cs:not_locked)

Lockable.exe!std::vector >::_Orphan_range(int * _First=0x0000c5db, int * _Last=0x0000c5db) 第 1442 行 + 0x5 字节 C++ Lockable.exe!std::vector >::push_back(const int & _Val=4177) 第 995 行 C++

Lockable.exe!anonymous namespace'::<lambda0>::operator()(int i=4177) Line 16 C++ Lockable.exe!Concurrency::_Parallel_chunk_helper_invoke<int,unsigned int,anonymous namespace'::,0>::_Invoke(const int & _First=0, unsigned int & _Index=4177, const anonymous-namespace'::<lambda0> & _Func={...}) Line 1445 C++ Lockable.exe!Concurrency::_Parallel_chunk_helper<int,unsigned int,anonymous namespace'::,0>::operator()() Line 1833 + 0x16 bytes C++ Lockable.exe!Concurrency::task_handle,0> > >(Concurrency::task_handle,0> > * _PChore=0x7cbffc24 {_M_first=0 _M_step=1 _M_function={...} ...}) Line 3495 C++ msvcr100d.dll!Concurrency::details::_UnrealizedChore::_StructuredChoreWrapper(Concurrency::details::_UnrealizedChore * pChore=0x7cbffc24 {_M_first=0 _M_step=1 _M_function={...} ...}) Line 99 + 0xc bytes C++ msvcr100d.dll!Concurrency::details::_UnrealizedChore::_Invoke() Line 3454 + 0xc bytes C++ msvcr100d.dll!Concurrency::details::WorkItem::Invoke() Line 75 C++ msvcr100d.dll!Concurrency::details::InternalContextBase::ExecuteChoreInline(Concurrency::details::WorkItem * pWork=0x7bc0fab4) Line 1385 C++ msvcr100d.dll!Concurrency::details::InternalContextBase::Dispatch(Concurrency::DispatchState * pDispatchState=0x7bc0fad4) Line 1478 C++ msvcr100d.dll!Concurrency::details::FreeThreadProxy::Dispatch() Line 157 C++ msvcr100d.dll!Concurrency::details::ThreadProxy::ThreadProxyMain(void * lpParameter=0x2dcf6200) Line 162 C++ kernel32.dll!763c33aa()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!771a9ef2()
ntdll.dll!771a9ec5()

甚至在 PPL 内部实现中也可能存在其他随机访问违规,我想您可以重现。 (希望如此)

我在 Visual Studio 2012 Express for Desktop 下运行了相同的程序,经过多次测试运行后,一切似乎都工作正常。

我想知道VS2010下的PPL在生产环境中使用是否有bug?

感谢您的任何意见!

最佳答案

关于c++ - PPL critical_section/parallel_for 在 Visual Studio 2010 下崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14490211/

相关文章:

c++ - 此排序数组代码导致最后一个元素消失

c++ - 海湾合作委员会警告 "dereferencing type-punned pointer will break strict-aliasing rules"

c++ - 为什么 auto 不能用作模板类型参数?

c# - 输入字符串的格式不正确

c++ - VC++ 允许为 STL 容器使用 const 类型。为什么?

c++ - 将我的 MMX Intrinsics 转换为 x64 (SSE) 我做错了什么?

c++ - MSVC 声称公共(public)成员不可访问

c++ - 可以在 constexpr 上下文中使用导致未指定(不是未定义!)行为的指针的表达式吗?

android - phonegap开发环境

c++ - 代码块构建的 Exe 比 Visual Studio 构建的相同代码大将近 57 倍