linuxmultithreadinggccc++11devtoolset

gcc 4.8.2 on Linux - Simple program using thread crashes


I have just installed gcc 4.8.2 on Centos (I am using devtoolset-2). I wrote a very simple program using thread. It compiles fine but crashes when executed?

 #include <thread>
 #include <iostream>

 void test() 
 { 
   std::cout << "test\n"; 
 }

 void main()
 {
      std::thread t(test);
      t.join();
      return 0;
 }

I compile with:

 scl enable devtoolset-2 bash
 c++ -o test test.cpp -std=c++11

I am terribly surprised. I must do something wrong, not using the write libc++ etc? Do you have any idea how I could debug this. Thank you! I compile it on Mac (Maverick) which obviously doesn't use gcc and it works fine.


Solution

  • On Linux, you should use the command line option -pthread with GCC and Clang for compiling and linking. In your case, the command line should look as follows:

    g++ -std=c++11 -Wall -Wextra -pthread test.cpp -o test
    

    See the following links for more information: