作者mmm321 (家家)
看板C_and_CPP
標題[問題] thread
時間Thu Jan 16 19:46:00 2014
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
最近開始學用thread,在網路上找了一些範例,
執行後卻出現Segmentation fault (core dumped),不知道是不是我忽略了什麼
範例來源:
http://ppt.cc/1bM6
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <iostream>
#include <thread>
using namespace std;
void test_func()
{
// do something
double dSum = 0;
for( int i = 0; i < 10000; ++ i )
for( int j = 0; j < 10000; ++ j )
dSum += i*j;
cout << "Thread: " << dSum << endl;
}
int main( int argc, char** argv )
{
// execute thread
thread mThread( test_func );
// do somthing
cout << "main thread" << endl;
// wait the thread stop
mThread.join();
return 0;
}
編譯: g++ -std=c++0x multithread.cpp -o mul
執行: ./mul
結果: Segmentation fault (core dumped)
麻煩板上大大幫我解答了,謝謝 :)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.112.33
→ Feis:g++ -std=c++11 multithread.cpp -o mul -lpthread 01/16 20:12
推 AstralBrain:-pthread 01/16 20:15
→ mmm321:謝謝!!!!! 01/16 20:21