删除std ::线程指针引发异常“libc ++ abi.dylib:terminating” [英] Deleting std::thread pointer raises exception "libc++abi.dylib: terminating"

查看:2245
本文介绍了删除std ::线程指针引发异常“libc ++ abi.dylib:terminating”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆