简体   繁体   English

为什么std :: make_unique <std::thread> (somefun())在我没有调用release()时会崩溃,而在调用时会崩溃吗?

[英]Why std::make_unique<std::thread>(somefun()) will generate crash when I haven't call release(), and well when called?

My code: 我的代码:

{
    t = std::make_unique<std::thread>(std::bind(&Widget::generateNum, this));
}

crash at ~thread() , error msg is ' r6010 abort() has been called '. 在撞车~thread()错误味精是“r6010中止()被调用 ”。 If I haven't call t.release() before that destructing t , will cause the crash. 如果在破坏t之前没有调用t.release() ,将会导致崩溃。

You must detach or join a thread before destroying it. 在销毁线程之前,必须分离或加入线程。

auto t = std::make_unique<std::thread>(std::bind(&Widget::generateNum, this));

// Either do this:
t->detach();
// or do this:
t->join();
// before *t gets destroyed.

The choice of whether to detach or join is up to you, but you must do one or the other. 决定是否分离还是加入取决于您,但是您必须执行其中一项。 If a non-empty std::thread gets destroyed it will call std::terminate , ending your program. 如果非空的std::thread被破坏,它将调用std::terminate ,结束程序。

See std::thread::~thread : 参见std :: thread :: ~~ thread

If *this has an associated thread ( joinable() == true ), std::terminate() is called. 如果*this具有关联的线程( joinable() == true ),则调用std::terminate()

The default std::terminate handler calls std::abort , hence the message that abort has been called. 默认的std::terminate处理程序调用std::abort ,因此已调用中止的消息。

Don't Call t.release() 不要致电t.release()

It would be a mistake to call t.release() . 调用t.release()是错误的。 This would leak the std::thread object. 这将泄漏std::thread对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 的std :: make_unique <std::thread> 带lambda - std::make_unique<std::thread> with lambda std :: make_unique会发生什么 <T> ()分配给std :: unique_ptr <T> ? - What happens when std::make_unique<T>() assigns to std::unique_ptr<T>? new T(...)vs。std :: make_unique <T> (…)。发布() - new T(…) vs. std::make_unique<T>(…).release() 为什么要为 std::make_unique 调用一组额外的构造函数/析构函数? - Why an extra set of constructor/destructor call for std::make_unique? 调用 std::make_unique 时出现分段错误 - Segmentation fault when calling std::make_unique 分配数组时是否可以将 arguments 传递给 std::make_unique() ? - Is it possible to pass arguments to std::make_unique() when allocating an array? 当包含std :: promise作为成员时,为什么结构的make_unique失败? - Why does make_unique of a struct fail when containing an std::promise as member? std::make_shared/std::make_unique 不使用列表初始化是否有原因? - Is there a reason why std::make_shared/std::make_unique don't use list initialization? 为什么std :: make_unique等人不存在std :: initializer_list重载? - Why don't std::initializer_list overloads exist for std::make_unique et al? &#39;make_unique&#39;不是&#39;std&#39;的成员 - 'make_unique' is not a member of 'std'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM