一 点睛
分离状态是线程一个很重要的属性。POSIX线程的分离状态决定了一个线程以什么样的方式终止自己。默认的分离状态是可连接,即我们创建线程时如果使用默认属性,则分离状态属性就是可连接的,因此,默认属性下创建的线程是可连接的。
POSIX下的线程要么是分离的,要么是可连接的。前者用宏PTHREAD_CREATE_DETACHED表示,后者用宏PTHREAD_CREATE_JOINABLEB表示。默认情况下创建的线程是可连接的,一个可连接的线程是可以被其他线程收回资源和杀死的,并且它不会主动释放资源,必须等待其他线程来收回其资源。因此我们要在主线程使用pthread_join函数,该函数是一个阻塞函数,当它返回时,所等待的线程资源也就释放了。再次强调,如果是可连接线程,当线程函数自己返回结束时,或调用pthread_exit结束时都不会释放线程所占用的堆栈和线程描述符(总计八千多个字节),必须调用pthread_join且返回后,这些资源才会被释放。
二 创建一个可分离线程
1 代码
#include <iostream>
#include <pthread.h>
#include <unistd.h> //sleep
using namespace std;
void *thfunc(void *arg)
{
cout<<("sub thread is running\n");
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t thread_id;
pthread_attr_t thread_attr;
struct sched_param thread_param;
size_t stack_size;
int res;
res = pthread_attr_init(&thread_attr); //初始化一个线程属性的结构体变量
if (res)
cout<<"pthread_attr_init failed:"<<res<<endl;
res = pthread_attr_setdetachstate( &thread_attr,PTHREAD_CREATE_DETACHED); //设置成可分离状态
if (res)
cout<<"pthread_attr_setdetachstate failed:"<<res<<endl;
res = pthread_create( &thread_id, &thread_attr, thfunc,NULL); // 将线程属性变量的地址作为参数传递给线程创建函数
if (res )
cout<<"pthread_create failed:"<<res<<endl;
cout<<"main thread will exit\n"<<endl;
sleep(1);
return 0;
}
2 运行
[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
main thread will exit
sub thread is running
3 说明
我们首先初始化一个线程属性结构体,然后设置其分离状态为PTHREAD_CREATE_DETACHED,并用这个属性结构体作为参数传入线程创建函数中。这样创建出来线程函数就是可分离线程。意味着该线程结束后,它所占用的任何资源都可以立刻被系统回收。程序的最后,然main线程挂起1秒,让子线程有机会执行。因为如果main线程很早就退出,将会导致整个进程很早退出,子线程就没机会执行了。
三 创建一个可分离线程,且main线程先退出
1 代码
#include <iostream>
#include <pthread.h>
using namespace std;
void *thfunc(void *arg)
{
cout<<("sub thread is running\n");
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t thread_id;
pthread_attr_t thread_attr;
struct sched_param thread_param;
size_t stack_size;
int res;
res = pthread_attr_init(&thread_attr); // 初始化线程结构体
if (res)
cout<<"pthread_attr_init failed:"<<res<<endl;
res = pthread_attr_setdetachstate( &thread_attr,PTHREAD_CREATE_DETACHED); //设置分离状态
if (res)
cout<<"pthread_attr_setdetachstate failed:"<<res<<endl;
res = pthread_create( &thread_id, &thread_attr, thfunc,NULL); // 创建一个可分离线程
if (res )
cout<<"pthread_create failed:"<<res<<endl;
cout<<"main thread will exit\n"<<endl;
pthread_exit(NULL); //主线程退出,但进程不会此刻退出,下面语句不会再执行
cout << "main thread has exited,this line will not run\n" << endl;
// 此句不会执行
return 0;
}
2 运行
[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
main thread will exit
sub thread is running
3 说明
main线程中调用了函数pthread_exit,将退出main线程,但进程并不会此刻退出,而是要等到子线程结束后才退出。因为是分离线程,它结束的时候,所占用的资源会立刻被系统回收。如果是一个可连接线程,则必须在创建它的线程中调用pthread_join来等待可连接线程结束并释放该线程所占的资源。