I am trying to create a new thread, offload execution to the new thread and kill main thread. This is the sample program.
#include <stdio.h>
#include <pthread.h>
void * main_thread(void * param) {
while (1) {
}
}
int main(int argc, char *argv[]) {
int result = 0;
pthread_attr_t attr;
pthread_t thread;
result = pthread_attr_init(&attr);
printf ("attr init : %d\n", result);
result = pthread_attr_setstacksize(&attr, 1024);
printf ("attr set stack: %d\n", result);
result = pthread_create (&thread, &attr, main_thread, NULL);
printf ("create new thread: %d\n", result);
result = pthread_detach(pthread_self());
printf ("detach main thread: %d\n", result);
pthread_exit (NULL);
return 0;
}
But this is leaving the thread (and process?) in defunct state.
ps -aef | grep threaded
user 204 306 9 10:20 pts/8 00:00:21 [threaded_progra] <defunct>
Then I found this - http://www.mentby.com/kaz-kylheku/main-thread-pthreadexitsysexit-bug.html
What is the reason for the issue? Is there a way to achieve the same thing without leaving the thread in a zombie/defunct state.