SIGABRT signal received when creating std :: thread C ++ 11

I create a stream in a class method, for example:

void MyClass::startThread() { T.reset( new std::thread( &MyClass::myThreadMethod, this ) ); } void MyClass::myThreadMethod() { // ... } 

Where

 // In header file std::unique_ptr<std::thread> T; 

When I run MyClass::startThread() , I get the following:

Received Signal: SIGABRT (Aborted) ...

If I find the code, this happens in the stream constructor.

I tried removing unique_ptr as follows:

 void MyClass::startThread() { std::thread* T = new std::thread( &MyClass::myThreadMethod, this ); } 

and the same thing happened. I am using gcc 4.8.2 on NetBeans 7.4 on Linux / Kubuntu 12.04.

Does anyone know what is going on?

+6
source share
2 answers

In accordance with the proposal and example of Marc Garcia and in accordance with this question, I simply added -pthread as an option for the compiler.

For some unknown reason, my other projects are working correctly, but I believe this is due to Boost or Open CV, which should include something missing from this current test.

In any case, at the moment it works.

Thanks!

+5
source

This happens when std::thread destroyed without first calling std::thread::detach() or std::thread::join() . You must call one of the two, and what to call depends on your desired behavior.

 void MyClass::startThread() { T.reset( new std::thread( &MyClass::myThreadMethod, this ) ); T->join(); // Wait for thread to finish } 

or

 void MyClass::startThread() { T.reset( new std::thread( &MyClass::myThreadMethod, this ) ); T->detach(); // Leave thread on its own (do not wait for it to finish) } 

As an additional note, you can remove your use of std::unique_ptr by making std::thread :

 class MyClass { std::thread t; }; 

To assign thread t , you can build it and move it to t :

 t = std::thread(&MyClass::myThreadMethod, this); 
+12
source

All Articles