[英]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
,因此已调用中止的消息。
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.