std::systemerror with thread application

Hi I am trying to multithread an extremely simple C++ application. I am going by C++ concurrency in action. I tried the example and it compiles but it throws an error on runtime. I tried modifying the code but the results are exactly the same.
This the error message:
1
2
3
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Hello... Aborted


This is C++11 so for those who do not already know, you need to use: -std=c++11 or -std=gnu++11 in order to compile.
My code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <thread>
void hello()
{
    printf("World!!\n");
}
int main()
{
    printf("Hello... ");
    std::thread thread1(hello); //create new thread.
    thread1.join(); //wait for "thread1" to finish
}
If you're using GNU g++, you have to provide another compiler option: -pthread. If you don't, you'll get the "operation not permitted" exception from thread creation.
Last edited on
I looked at the another post where you said given the same advice. I tried it but it the result is exactly the same.
Last edited on
Oh, I found the problem. -pthread is incorrect. It needs to be: -lpthread for it work properly.
What's your compiler and platform, exactly?
With GNU g++ 4.7.2 on Linux, specifically -pthread is the correct option. It is essentially equivalent to -D_REENTRANT -lpthread (so by only specifying the library, you're missing out on the preprocessor and library awareness), but there might be additional nuances.
I am running Fedora 18 with g++ 4.7.2 and I have a 64bit amd processor, quad core.
Topic archived. No new replies allowed.