4

Possible Duplicate:
Why does this simple std::thread example not work?

Code:

#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();
}

Issue:

$ 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

"Abortado" means "aborted" in my locale.

Community
  • 1
  • 1
lvella
  • 12,754
  • 11
  • 54
  • 106

1 Answers1

9

You should link it to pthread:

g++ -o thread_test thread_test.cpp -std=c++0x -lpthread

libstdc++'s std::thread implementation requires you to link your applications to libpthread, otherwise they'll throw a std::system_error when you try to create a thread.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
  • 4
    Can somebody please link to a bug report or email chain or whatever where somebody justifies as to why this isn't thrown as a linker error? How is this not a linker error that should be caught then? – Kevin Anderson Nov 19 '12 at 19:25
  • @Kevin Very interesting question. I can't really find any answers online though. You might want to ask that question on a new thread, rather than just in a comment, to see if someone gives you a hint. – mfontanini Nov 19 '12 at 19:33