嗚嗚喔學習筆記: C++ 多執行序[Thread][錯誤:abort() has been called]

搜尋此網誌

2013年12月1日 星期日

C++ 多執行序[Thread][錯誤:abort() has been called]

 最近寫了C++ 多執行序程式遇到了些錯誤~~~在此紀錄

錯誤紀錄:















錯誤: abort() has been called 
       又再一次釋放執行序

原因:  thread mThread 宣告在 if(ture) 後 導致再{}結束後 自動釋放 mThread 
        在執行序執行完畢後又會再釋放一次 mThread 導致錯誤

更正方法: 把thread 宣告在 if(){}區域前就能解決了~

更正程式碼為:











完整程式碼:


#include <iostream>
#include <thread>//使用thread 需要的include

using namespace std;

void test_func(int number)
{
    //寫個大迴圈拖延時間
    double dSum = 0;
    for( int i = 0; i < 10000; ++ i )
      for( int j = 0; j < 10000; ++ j )
        dSum += i*j;
    cout << "Thread:01   " << number << endl;
}

int main( int argc, char** argv )
{
  // 宣告thread 新執行序
  thread mThread;
  if(true)
     mThread=thread(test_func,100);//(fun名稱,fun參數)
 
  //輸出main函式 觀看執行序是否運作
  cout << "main02" << endl;

  // 等待 mthread 結束在執行後續程式碼 
  mThread.join();

  cout << "main03" << endl;

  system("pause");

  return 0;

}

執行結果: 依序為 02 01 03 證明新執行序執行較速度較慢 才會造成 02 比 01 先輸出的結果














沒有留言:

張貼留言