When the main thread returns (i.e., you return from the main
function), it terminates the entire process. This includes all other threads. The same thing happens when you call exit
. You can avoid this by calling pthread_exit
.
The purpose of pthread_detach
is to make it so you don’t need to join with other threads in order to release their resources. Detaching a thread does not make it exist past process termination, it will still be destroyed along with all the other threads.