C ++ 11 thread error runtime

advertisements

Hello I have problem with threads in C++11. I have ubuntu 64bit 13.10(testing) with g++ 4.8.1. I tried to compile code:

#include <thread>

void func()
{
   // do some work
}

int main()
{
   std::thread t(func);
   t.join();
   return 0;
}

with options: -std=c++11 -pthread -lpthread. Compilation was successful, but when I tried to run it, I've received an error:

terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted


I think the other answers are a bit misleading. What is important is that you only need -pthread. The order of this flag is not important!

-pthread will automatically link with libpthread and it'll do so correctly. Note that you need to provide this option both when compiling and linking your code (except when you do everything at once, of course).

Only when you provide -lpthread explicitly, the order of where you put might be important, but as already mentioned, you shouldn't add it explicitly when using -pthread.