創建線程時出現分段錯誤我是C++多線程新手。 我寫了一個簡單的程序來使用線程打印hello world。使用#include <thread>
<<mythread.cpp>>
#include<iostream>
#include<thread>
using namespace std;
void hello()
{
std::cout<<"Hi this is a thread";
}
int main()
{
std::thread mythread(hello);
cout<<'1';
if (mythread.joinable())
{
cout<<'2';
mythread.join();
cout<<'3';
}
return 0;
}
Copilation命令:克++ -std =的C++ 0x mythread.cpp
它編譯成功但放棄Segmentaion故障在運行時。
我檢查覈心文件:
(gdb) bt
#0 0x0000003ac340df7c in _dl_fixup() from /lib64/ld-linux-x86-64.so.2
#1 0x0000003ac3414625 in _dl_runtime_resolve() from /lib64/ld-linux-x86-64.so.2
#2 0x0000003ac84b65c7 in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>)() from /usr/lib64/libstdc++.so.6
#3 0x00000000004010d0 in std::thread::thread<void (*)()>(void (*)())()
#4 0x0000000000400e15 in main()
請幫我解決這個錯誤似乎有些庫不支持。
我相信GCC需要使用-pthread'命令行選項'和''工作時。 –
Angew
確切地說,當你運行'g ++ -std = C++ 0x -o mythread mythread.cpp'時,'g ++'應該給你鏈接器錯誤,而不用'-pthread'。您的問題不可重現,因爲您的命令行不會生成可執行文件(但是無法生成可執行文件)。 –