【问题标题】:Pthreads - Can I detach from a thread and then join in main?Pthreads - 我可以从线程中分离然后加入主线程吗?
【发布时间】:2017-01-03 15:01:29
【问题描述】:

我想做的是...

pthread_create(&producer_thread, &to_join, producer_routine, &queue);
pthread_detach(producer_thread);
...
...
pthread_join(producer_thread, NULL);

这是否可能,在运行上述代码时,它无法加入线程。

【问题讨论】:

  • 你熟悉XY problem吗?
  • 没有。分离线程使其永久不可连接。但是,如果您可以控制线程功能,那么您可以使用自己的机制来等待特定的分离线程。
  • 阅读man page 总是一个好主意:“一旦线程被分离,它就不能用 pthread_join(3) 加入或再次加入。”

标签: c multithreading pthreads mutex pthread-join


【解决方案1】:

一旦分离,就无法再加入。

来自pthread_detach() 手册页上的注释

线程一旦分离,就不能再加入 pthread_join(3) 或再次加入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    相关资源
    最近更新 更多