1

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.

user1912491
  • 101
  • 6
  • 2
    It seems that the issue that prevents a fix is what Ulrich Drepper says in the conversation thread you linked to: "If the patch changes the behavior that the main thread, after calling sys_exit, still react to signals sent to this thread or to the process as a whole, then the patch is wrong. The userlevel context of the thread is not usable anymore. It will have run all kinds of destructors. The current behavior is AFAIK that the main thread won't react to any signal anymore. That is absolutely required." – Michael Burr Jan 25 '13 at 05:59

0 Answers0