title: PPL critical_section/parallel_for crashing under visual studio 2010?
body: <p>I've made this simple test program, and it sometimes triggers a Debug Break or it just crashes (under Debug/Win32/VS2010SP1) - and of course, sometimes it even works. Is there anything I am doing wrong or is there a bug somewhere in PPL (VS2010)?</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;ppl.h&gt;
  #include &lt;vector&gt;
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      std::vector&lt;int&gt; vi;
      Concurrency::critical_section cs;
      Concurrency::parallel_for(0, 10000, [&amp;](int i)
      {
          Concurrency::critical_section::scoped_lock l(cs);
          vi.push_back(i);
      });
      return 0;
  }
  </code></pre>
  
  <p>The Debug Break call stack looks like below:</p>
  
  <blockquote>
    <p>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 &amp; _Critical_section=locked)  Line 1083  C++
        Lockable.exe!<code>anonymous namespace'::&lt;lambda0&gt;::operator()(int i=1418)  Line 14 + 0x11 bytes C++
        Lockable.exe!Concurrency::_Parallel_chunk_helper_invoke&lt;int,unsigned int,</code>anonymous namespace'::,0>::_Invoke(const int &amp; _First=0, unsigned int &amp; _Index=1418, const <code>anonymous-namespace'::&lt;lambda0&gt; &amp; _Func={...})  Line 1445    C++
        Lockable.exe!Concurrency::_Parallel_chunk_helper&lt;int,unsigned int,</code>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()<br>
        [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]<br>
        ntdll.dll!771a9ef2()<br>
        ntdll.dll!771a9ec5()    </p>
  </blockquote>
  
  <p>One crash I've just got looks like the lock has not been held (cs: not_locked)</p>
  
  <p>Lockable.exe!std::vector >::_Orphan_range(int * _First=0x0000c5db, int * _Last=0x0000c5db)  Line 1442 + 0x5 bytes  C++
   Lockable.exe!std::vector >::push_back(const int &amp; _Val=4177)  Line 995 C++</p>
  
  <blockquote>
    <p>Lockable.exe!<code>anonymous namespace'::&lt;lambda0&gt;::operator()(int i=4177)  Line 16  C++
        Lockable.exe!Concurrency::_Parallel_chunk_helper_invoke&lt;int,unsigned int,</code>anonymous namespace'::,0>::_Invoke(const int &amp; _First=0, unsigned int &amp; _Index=4177, const <code>anonymous-namespace'::&lt;lambda0&gt; &amp; _Func={...})  Line 1445    C++
        Lockable.exe!Concurrency::_Parallel_chunk_helper&lt;int,unsigned int,</code>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()<br>
        [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]<br>
        ntdll.dll!771a9ef2()<br>
        ntdll.dll!771a9ec5()    </p>
  </blockquote>
  
  <p>And there could be other random access violations even inside the PPL internal implementation, that I guess you could be able to reproduce. (hopefully)</p>
  
  <p>I ran the same program under Visual Studio 2012 Express for Desktop, and everything seems to be working properly after many test runs.</p>
  
  <p>I'm wondering that if PPL under VS2010 is buggy to be used in a production environment?</p>
  
  <p>Thanks for any inputs!</p>