假设我有一个小操作,我想在单独的线程中执行。我不需要知道它何时完成,也不需要等待它完成,但我不希望该操作阻塞我当前的线程。当我编写以下代码时,我会崩溃:
void myFunction() {
// do other stuff
std::thread([]()
{
// do thread stuff
});
}
通过将线程分配给变量并将其分离来解决此崩溃:
void myFunction() {
// do other stuff
std::thread t([]()
{
// do thread stuff
});
t.detach();
}
为什么这一步是必要的?或者有更好的方法来创建一个小型的一次性线程吗?
最佳答案
Because the std::thread::~thread()
specification says so :
A thread object does not have an associated thread (and is safe to destroy) after
- it was default-constructed
- it was moved from
- join() has been called
- detach() has been called
看起来 detach()
是其中唯一对您的情况有意义的方法,除非您想将线程对象(通过移动)返回给调用者。
Why is this step necessary?
考虑线程对象代表一个长时间运行的执行“线程”(轻量级进程或内核可调度实体或类似实体)。
允许您在线程仍在执行时销毁对象,使您无法随后加入该线程(并查找其结果)。这可能是一个逻辑错误,但它也可能使正确退出程序变得困难。
Or is there a better way to create a small single-use thread?
并不明显,但使用线程池在后台运行任务通常比启动和停止大量短期线程更好。
您也许可以使用std::async()
相反,但在某些情况下,如果您尝试丢弃它,它返回的future
可能会在析构函数中阻塞。
关于c++ - 为什么我需要显式分离短期变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51480720/