简单的boost ::线程使用崩溃

时间:2013-11-19 14:40:37

标签: boost-thread

此代码崩溃,有时:

#include <boost/thread.hpp>
struct ThreadData 
{
   int t,f; // dummy, but without this it's not crashing
   int number;
};

// this is what each thread does...
void MyThreadFunction(ThreadData* threadData) 
{ 
   for (int i=0;i<100000000;++i)
   {
      threadData->number++; // without this it's not crashing
   }
}

int main()
{       
   const int MaxThreads(5);
   ThreadData threadData[MaxThreads];// if it's global app is not crashing
   boost::thread hThreadArray[MaxThreads]; 
   for( int i=0; i<MaxThreads; i++ )
   {
      hThreadArray[i] = boost::thread(MyThreadFunction,threadData+i);
   } 

   boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
   for(int i=0; i<MaxThreads; i++)
   {
      hThreadArray[i].detach();
   }
boost::this_thread::sleep_for(boost::chrono::milliseconds(1000)); // wait for threads to die
}

我正在使用win32,MSVC2012,boost1.52。 当我把threadData全局化,或者删除ThreadData中的虚拟整数时,没关系。 它在主线程上失败,访问冲突执行某个地址。 当它崩溃时,仍有2个线程仍处于活动状态。 有什么想法吗?

0 个答案:

没有答案