为什么这个程序抛出'std :: system_error'?

lve*_*lla 4 c++ c++11 stdthread

可能重复:
为什么这个简单的std :: thread示例不起作用?

码:

#include <iostream>
#include <thread>

void f()
{
  std::cout << "hi thread" << std::endl;
}

int main()
{
  std::thread t(f);
  std::cout << "hi" << std::endl;
  t.join();
}
Run Code Online (Sandbox Code Playgroud)

问题:

$ g++ -o thread_test thread_test.cpp -std=c++0x
$ ./thread_test         
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Abortado
Run Code Online (Sandbox Code Playgroud)

"Abortado"在我的语言环境中意为"中止".

mfo*_*ini 9

你应该把它链接到pthread:

g++ -o thread_test thread_test.cpp -std=c++0x -lpthread
Run Code Online (Sandbox Code Playgroud)

libstdc++std::thread实现需要你链接你的应用程序libpthread,否则他们会std::system_error在你尝试创建一个线程时抛出一个.

  • 有人可以链接到错误报告或电子邮件链或任何有人证明为什么不作为链接器错误抛出的东西?这怎么不是应该被捕获的链接器错误呢? (4认同)