我正在尝试创建一个新线程,将执行卸载到新线程并终止主线程。这是示例程序。
#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;
}
但这会让线程(和进程?)处于不存在状态。
ps -aef | grep threaded
user 204 306 9 10:20 pts/8 00:00:21 [threaded_progra] <defunct>
然后我发现了 - http://www.mentby.com/kaz-kylheku/main-thread-pthreadexitsysexit-bug.html
问题的原因是什么?有没有办法实现同样的事情,而不会让线程处于僵尸/不存在的状态。