Deleting std::thread pointer raises exception “libc++abi.dylib: terminating”

前端 未结 1 1621
迷失自我
迷失自我 2021-01-20 00:01

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

std::thread* th = new std::thread([&] (int tid) {
          


        
1条回答
  •  -上瘾入骨i
    2021-01-20 00:29

    The thread you've created is joinable, and unless you join or detach it, std::terminate will be called when the destructor of the thread object executes. So you need

    th->join();
    delete th;
    

    Early proposals for std::thread implicitly detached the thread in the destructor, but this was found to cause problems when the owning thread threw an exception between creation and joining of a thread instance. N2802 contains the change proposal along with illustrative examples.

    The original behavior was carried over from boost::thread but it too has since deprecated implicit detach in the destructor.


    Unrelated to your problem, but it is very unlikely you need to dynamically allocate the thread object, and even if you do, you should be storing it in a unique_ptr.

    0 讨论(0)
提交回复
热议问题