线程可分为两种,一种是可连接的(joinable),另一种是不可连接的(unjoinable),也就是分离的(detached)。
在Linux下,通过pthread_create()创建的线程默认是可连接的,当线程退出时,系统不会去释放该线程的资源。这时,需要pthread_join()去获取该线程的状态,并释放其占用的资源。如果一个可连接的线程在退出后不被连接,将会产生僵尸线程(zombie thread),僵尸线程的资源无法释放,如果僵尸线程积累过多,进程将无法再创建新的线程。
1. pthread_join()
pthread_join()等待由thread标识的线程终止,如果线程已经终止,pthread_join()会立即返回。这种操作被称为线程的连接。
pthread_join()函数定义如下:
include<pthread.h>
int pthread_join(pthread_t thread, void **retval); //Return 0 on success, or a positive error number on error
其中,thread是进程内部线程的唯一标识,称为线程ID,该线程ID值在会返回给pthread_create()的调用线程(即创建该线程的线程),某个线程也可以通过pthread_self()来获取自己的线程ID。
retval为一指针,会保存线程终止时返回值的拷贝,该返回值也就是线程执行return或pthread_exit()时所指定的值。
一个特定ID的线程只能被连接一次,如向pthread_join()传入一个之前已经连接过的线程ID,将会导致无法预知的行为。
pthread_joint()以阻塞的方式等待指定ID的线程返回,也就是说,在等待的线程返回前,调用pthread_join()的线程无法继续往下执行,看一个示例:
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
void *myFunc(void *arg)
{
for(int i = 0; i < 10; i++)
{
printf("Loop num = %d \n", i);
usleep(500000); //sleep 500 ms
}
return NULL;
}
int main(void)
{
pthread_t myThread;
int ret;
ret = pthread_create(&myThread, NULL, myFunc, NULL);
if(ret != 0)
{
printf("Error create thread! \n");
exit(1);
}
ret = pthread_join(myThread, NULL);
if(ret != 0)
{
printf("Error join thread! \n");
exit(1);
}
printf("This is the end of main func. \n");
return 0;
}
执行结果:
由执行结果可以看到,在线程函数myFunc()返回前,主线程一直被阻塞,直到myFunc()执行完,才执行了主线程的打印语句。
在以上例子中,如果我们不调用pthead_join(),看看会发生什么情况:
主函数代码:
int main(void)
{
pthread_t myThread;
int ret;
ret = pthread_create(&myThread, NULL, myFunc, NULL);
if(ret != 0)
{
printf("Error create thread! \n");
exit(1);
}
#if 0
ret = pthread_join(myThread, NULL);
if(ret != 0)
{
printf("Error join thread! \n");
exit(1);
}
#endif
sleep(1); //为了直观,我们在主线程返回之前,先睡眠1秒钟
printf("This is the end of main func. \n");
return 0;
}
执行结果:
可见,如果不调用pthread_join,主线程不会被阻塞,线程函数myFunc执行了两个loop后,主线程睡眠1秒的时间过去,立即执行了打印语句并退出。
2. pthread_detach()
如果我们不关心线程退出时的状态,只需要线程在退出时,所占用的资源能够被系统自动释放,那么,可以将线程设置为非连接状态,即分离状态(detached)。函数pthread_detach()可实现这一目标:
#include <pthread.h>
int pthread_detach(pthread_t thread); //Return 0 on success, or a positive error number on error
一旦线程处于分离状态,就不能再使用pthread_join()来获取其状态,也无法使其重返可连接(joinable)状态。一个线程也可以通过调用pthread_detach(pthread_self())来将自己设置成分离属性。注意,pthread_detach()不会阻塞主调线程的执行。我们上例中,pthread_join()去掉,代之以pthread_detach(),看看是什么结果:
int main(void)
{
pthread_t myThread;
int ret;
ret = pthread_create(&myThread, NULL, myFunc, NULL);
if(ret != 0)
{
printf("Error create thread! \n");
exit(1);
}
ret = pthread_detach(myThread);
if(ret != 0)
{
printf("Error detach thread! \n");
exit(1);
}
printf("pthread_detach is called. \n");
sleep(8);
printf("This is the end of main func. \n");
return 0;
}
执行结果:
可见,创建myFunc线程后,主线程继续往下执行,调用了打印语句打印出“pthread_detach is called.”,然后睡眠8秒,使得线程myFunc有足够时间执行完,8秒后,执行了第二条打印语句并退出。
如果在detached的状态下,再调用pthread_join(),会是什么结果?
nt main(void)
{
pthread_t myThread;
int ret;
ret = pthread_create(&myThread, NULL, myFunc, NULL);
if(ret != 0)
{
printf("Error create thread! \n");
exit(1);
}
ret = pthread_detach(myThread);
if(ret != 0)
{
printf("Error detach thread! \n");
exit(1);
}
ret = pthread_join(myThread, NULL);
if(ret != 0)
{
printf("Error join thread! \n");
exit(1);
}
printf("pthread_detach is called. \n");
sleep(8);
printf("This is the end of main func. \n");
return 0;
}
执行结果:
pthread_join()调用出错,直接退出,并且被创建线程的打印也出现异常(在其他直接退出的情况下,也会出现这种异常,非调用pthread_join()导致)。