Trying to set up a small client-server program. However, I've noticed output from the second thread doesn't get out printed.
void *ClientRoutine(void *args){
printf("Client thread created\n");
pthread_exit(NULL);
}
int main(int argc, char *argv[]){
pthread_t client_thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&client_thread, &attr, &ClientRoutine, NULL)) {
perror("Failed to create client thread");
exit(-1);
}
pthread_attr_destroy(&attr);
return 0;
}
If I printf something in the main thread, the client's output finally shows up. Can anyone explain why this is happening?
LE: as some of you have suggested, this does happen because the main thread exits early. However, according to this: What happens to a detached thread when main() exits?, the detached thread should continue execution even after main exits. Why does this happen?