Removing std :: thread pointer throws "lib ++ abi.dylib: terminating" exception

In C ++ 11 with LLVM 6.0 on Mac OS X, I first created a pointer to std :: thread memory allocation.

std::thread* th = new std::thread([&] (int tid) {
    // do nothing.
}, 0);

      

Then I tried to remove it.

delete th;

      

However, when compiling the above code and executing it, an exception is thrown

libc++abi.dylib: terminating
Abort trap: 6

      

+3


source to share


1 answer


The thread you created joinable

, and if you join

or detach

it, std::terminate

will be called when the destructor of the thread object. Therefore you need

th->join();
delete th;

      


Early suggestions for std::thread

implicitly detach

editing the stream in the destructor, but this was found to cause problems when the personal resource threw an exception between join

instantiation and ing thread

. N2802 contains an amendment proposal along with illustrative examples.



The original behavior carried over from boost::thread

, but it has also since been deprecated implicitly detach

in the destructor.


Not related to your problem, but it is highly unlikely that you need to dynamically allocate the stream object, and even if you do, you should store it in unique_ptr

.

+14


source







All Articles