是否允许调用join()
在 std::thread
如果对象在线程之间正确同步,那么在不同线程中创建的对象?
例如:
void cleanup(std::thread t)
{
t.join();
}
int main()
{
std::thread t{[] { /* something */ }};
std::thread c{cleanup, std::move(t)};
c.join();
}
最佳答案
是的。 join()
除了线程本身之外,任何线程都可以在有效的线程对象上调用。
Error Conditions
resource_deadlock_would_occur if
this->get_id() == std::this_thread::get_id()
.no_such_process if the thread is not valid
invalid_argument if
joinable()
is false
关于c++ - 在线程 A 中创建 std::thread 对象,加入线程 B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59479272/