【问题标题】:Create a std::thread object in thread A, join in thread B在线程 A 中创建一个 std::thread 对象,加入线程 B
【发布时间】:2020-04-16 03:44:55
【问题描述】:

是否允许在另一个线程中创建的std::thread 对象上调用join(),前提是该对象在线程之间正确同步?

例如:

void cleanup(std::thread t)
{
    t.join();
}

int main()
{
    std::thread t{[] { /* something */ }};

    std::thread c{cleanup, std::move(t)};
    c.join();
}

【问题讨论】:

    标签: c++ multithreading c++11 thread-safety


    【解决方案1】:

    是的。 join() 可以由任何线程在有效线程对象上调用,但线程本身除外。

    错误情况

    resource_deadlock_would_occur if this->get_id() == std::this_thread::get_id().

    no_such_process 如果线程无效

    invalid_argument 如果joinable() 为假

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      相关资源
      最近更新 更多