我可以在 pthread_detached 线程上调用 pthread_join 吗?

Can I call pthread_join on a pthread_detached thread?

我有 16 个线程从一个 parent 线程派生。我正在等待 children 通过在 parent 中调用 pthread_join 来完成执行。然而,之后在每个 children 线程中我调用 pthread_detach。我想知道 parent 是否仍会等待 children 完成执行。在 pthread_detach 的手册页中说:“当一个分离的线程终止时,它的资源是 自动释放回系统而不需要 另一个线程与已终止的线程连接。"

Can I call pthread_join on a pthread_detached thread?

没有

I'm wondering if the parent will still wait for the children to finish executing.

这是未定义的行为。

pthread_join 说:

The behavior is undefined if the value specified by the thread argument to pthread_join() does not refer to a joinable thread.

分离的线程不可连接。

如果一个子线程将自身更改为分离状态,而另一个线程正在尝试加入它,则您现在正在加入一个不可加入的线程。

您应该为每个线程调用 pthread_joinpthread_detach 最多一次。不要为同一个线程调用两者。