|
不知道这个结论对不对,昨天在写单元测试代码的时候有线程相关的一个测试的东西,然后隔壁的问了一下这个问题,当主线程退出的时候,这个进程的其他线程还会继续跑吗?
系统是Redhat as 4.0
#include <pthread.h>
#include <stdio.h>
/*Prints x ’s to stderr.The parameter is unused.Does not return.*/
void*print_xs (void*unused)
{
while (1)
{
fputc (‘x ’,stderr);
}
return NULL;
}
/*The main program.*/
int main ()
{
pthread_t thread_id;
/*Create a new thread.The new thread will run the print_xs
function.*/
pthread_create (&thread_id,NULL,&print_xs,NULL);
/*Print o ’s continuously to stderr.*/
while (1)
{
fputc (‘o ’,stderr);
}
return 0;
}
如上这个程序,这样跑是没有问题的,创建的线程是不会被回收掉的,但是把main 函数改成
/*The main program.*/
int main ()
{
pthread_t thread_id;
/*Create a new thread.The new thread will run the print_xs
function.*/
pthread_create (&thread_id,NULL,&print_xs,NULL);
/*Print o ’s continuously to stderr.*/
return 0;
}
这样的话就直接执行完线程创建然后退出之后创建的这个线程也被清掉回收了.
不是很了解这个线程库的处理,有人能解释一下吗? |
|