Skid
Skid

Reputation: 95

What Happens If I Detach A Thread That Has Been Joined?

Question is fairly self explanatory, but here is the context, basically I have a server socket thread that spawns child threads when it receives new connections. These child threads accept data dumps from the remote connections, then clean up themselves and close when they are done.

Currently I have the child threads calling "pthread_detach(pthread_self())" right before they exit, what I'm considering doing is making the program to wait on program close, for the active data dumps to finish. Now I actually already have an alternate way around this that's part of the dynamic array I'm using to keep track of the active threads, but for future reference I would like to know what would happen if you joined a thread destined to detach itself before it closes and if it'll cause any issues.

Upvotes: 2

Views: 72

Answers (1)

this
this

Reputation: 5290

This is what the documentation says.

If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an [ESRCH] error. ( Is listed for both functions. )

If you join a detached thread you should get an error returned.

The same happens if you detach a joined thread.

Upvotes: 1

Related Questions