在Mac OS X上使用LLVM 6.0的C ++ 11中,我首先创建了一个指向std :: thread的内存分配的指针。
std :: thread * th = new std :: thread([&](int tid){ // do nothing。},0);然后我试图删除它。
delete th;但是,编译上面的代码并执行它会引发异常 $ b libc ++ abi.dylib:terminating 中止陷阱:6解决方案
您创建的主题是 joinable ,除非您 加入 或 detach , std :: terminate com / w / cpp / thread / thread /〜thread>析构函数。所以你需要
th-> join(); delete th;
std的早期提案:: thread implicitly 分离在析构函数中的线程,但是这被发现导致问题时,拥有线程抛出一个异常创建和加入线程实例的 N2802 包含更改提议及说明性示例。 p>
原来的行为从 boost :: thread 继承,但它也有已淘汰在析构函式中隐藏 detach 。
与您的问题无关,但不太可能需要动态分配线程对象,即使您,您应该将它存储在 unique_ptr 中。
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) { // do nothing. }, 0);Then I tried to delete it.
delete th;However, compiling the above code and execute it raises exception
libc++abi.dylib: terminating Abort trap: 6解决方案
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.
更多推荐
删除std ::线程指针引发异常“libc ++ abi.dylib:terminating”
发布评论