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

后端 未结 5 616
无人及你
无人及你 2020-12-01 02:41

Tried the following example compiled with g++ -std=gnu++0x t1.cpp and g++ -std=c++0x t1.cpp but both of these result in the example aborting.

5条回答
  •  感情败类
    2020-12-01 03:30

    You have to join std::threads, just like you have to join pthreads.

    int main( int argc, char *argv[] )
    {
        std::thread t( doSomeWork );
        t.join();
        return 0;
    }
    

    UPDATE: This Debian bug report pointed me to the solution: add -pthread to your commandline. This is most probably a workaround until the std::thread code stabilizes and g++ pulls that library in when it should (or always, for C++).

提交回复
热议问题