Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIGABRT signal received when creating a std::thread c++11

I create a thread in a class member method like this:

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 receive this:

Signal received: SIGABRT (Aborted) ...

If I step the code, it happens in the thread constructor.

I tried to removed the unique_ptr like this:

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

and the same thing occurred. I use gcc 4.8.2 on NetBeans 7.4 on Linux/Kubuntu 12.04.

Someone knows what happens?

like image 930
dom_beau Avatar asked Feb 07 '14 04:02

dom_beau


People also ask

What is SIGABRT signal?

Signal 5 ( SIGTRAP ) = “General crash” Signal 6 ( SIGABRT ) = SIGABRT is commonly used by libc and other libraries to abort the program in case of critical errors. For example, glibc sends an SIGABRT in case of a detected double-free or other heap corruptions.

What causes SIGABRT C++?

SIGABRT errors are caused by your program aborting due to a fatal error. In C++, this is normally due to an assert statement in C++ not returning true, but some STL elements can generate this if they try to store too much memory.

What is SIGABRT error in C++?

A SIGABRT (signal abort) error means that the app was deliberately crashed due to some really bad problem, like a runtime error during the start-up sequence or a bad or unreadable user interface file.

What sends SIGABRT?

The SIGABRT signal is sent to a process to tell it to abort, i.e. to terminate. The signal is usually initiated by the process itself when it calls abort function of the C Standard Library, but it can be sent to the process from outside like any other signal.


2 Answers

This happens when an std::thread is destroyed without a prior call to std::thread::detach() or std::thread::join(). You should call either 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 a side note, you can remove your use of std::unique_ptr by making the std::thread itself a member:

class MyClass {
    std::thread t;
};

To assign t a thread, you can construct one and move assign it to t:

t = std::thread(&MyClass::myThreadMethod, this);
like image 96
Mark Garcia Avatar answered Oct 30 '22 11:10

Mark Garcia


According to Mark Garcia suggestion and example, and according to this question I just added -pthread as option to the compiler.

For an unknown reason, my other projects work correctly but I believe that it is due to Boost or Open CV that must include something that was missing from this current test.

Anyway, for the moment, it works.

Thanks!

like image 23
dom_beau Avatar answered Oct 30 '22 11:10

dom_beau