删除std ::线程指针引发异常“libc ++ abi.dylib:terminating”

编程入门 行业动态 更新时间:2024-10-08 00:24:26
本文介绍了删除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”

本文发布于:2023-11-26 19:14:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   指针   异常   std   terminating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!