My code:
{
t = std::make_unique<std::thread>(std::bind(&Widget::generateNum, this));
}
crash at ~thread()
, error msg is 'r6010 abort() has been called'. If I haven't call t.release()
before that destructing t
, will cause the crash.
My code:
{
t = std::make_unique<std::thread>(std::bind(&Widget::generateNum, this));
}
crash at ~thread()
, error msg is 'r6010 abort() has been called'. If I haven't call t.release()
before that destructing t
, will cause the crash.
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.
See std::thread::~thread:
If
*this
has an associated thread (joinable() == true
),std::terminate()
is called.
The default std::terminate
handler calls std::abort
, hence the message that abort has been called.
It would be a mistake to call t.release()
. This would leak the std::thread
object.