linux exit 主线程,linux下pthread_exit在主线程中的用途

1.pthread_exit在main中有只终止main线程,而不终止整个进程的作用(注意不存在父子线程的概念),下面是posix中关于pthread_exit的一段描述:

When you program with POSIX Threads API,there is one thing about pthread_exit() that you may ignore for mistake. In subroutines that complete normally, there is nothing special you have to do unless you want to pass a return code back using pthread_exit(). The completion won't affect the other threads which were created by the main thread of this subroutine. However, in main(), when the code has been executed to the end,there could leave a choice for you. If you want to kill all the threads that main() created before, you can dispense with(无需) calling any functions. But if you want to keep the process and all the other threads (except for the main thread ) alive after the exit of main(), then you can call pthread_exit() to realize it. And any files opened inside the main thread will remain openafter its termination.

这段话的意思就是:在main线程终止时如果调用了pthread_exit(),那么此时终止的只是main线程,而进程的资源会为其他由main线程创建的线程保持打开的状态,直到其他线程都终止。而在其他的由main线程创建的线程中pthread_exit并没有这种作用。

下面给出实例,编译方式: g++ pthread_exit.cpp -o pthread_exit -lpthread

//pthread_exit.cpp

#include

#include

#include

int which1=1;

int which2=2;

void * cchild_thread(void*arg){

int i=0;

while(i++<5){

sleep(2);

std::cout<

}//while

}//cchild_thread

void* child_thread1(void*arg){

sleep(1);

std::cout<

pthread_t tid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

pthread_create(&tid,&attr,cchild_thread,(void*)which1);

int a=0;

while(a++<3)

std::cout<

pthread_t tid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

pthread_create(&tid,&attr,cchild_thread,(void*)which2);

int a=0;

while(a++<3)

std::cout<

//pthread_exit(1);

std::cout<

pthread_exit((void*)1);

}

2.此时主线程是fork生成的子进程中的线程,如果fork生成的子进程死亡,那么其下面的线程也会死亡:

//fork_thread.cpp : g++ fork_thread.cpp -o fork_thread -lpthread

#include

#include

#include

#include

int which1=1;

std::ifstream fIn;

void * cchild_thread(void*arg){

int i=0;

while(i++<5){

sleep(2);

std::cout<

}//while

std::string mid;

if(fIn>>mid)

std::cout<

else std::cout<

}//cchild_thread

void* child_thread1(void*arg){

fIn.open("test.txt");

if(!fIn)

std::cout<

else

std::cout<

sleep(1);

std::cout<

pthread_t tid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

pthread_create(&tid,&attr,cchild_thread,(void*)which1);

int a=0;

while(a++<3)

std::cout<

int iret=0;

if((iret=pthread_create(&tid,&attr,child_thread1,(void *)a))!=0){

perror("Create thread error");

}else{

std::cout<

}

std::cout<

std::cout<

pthread_exit((void*)1);

}else if(p1>0){

std::cout<

char tmp;

do{

std::cin>>tmp;

sleep(1);

}while(tmp!='q');

}

}//main

3.

如下面的情况,在进程中调用(即在主线程中调用)pthread_exit,那么主线程将等待线程1、2、3、4。总之线程没有父子关系,都是共享创建其的进程的数据。

0818b9ca8b590ca3270a3433284dd417.png

代码如下:

//g++ pthread_exit.cpp -o pthread_exit -lpthread

#include

#include

#include

int which1=1;

int which2=2;

void * cchild_thread(void*arg){

int i=0;

while(i++<5){

sleep(2);

std::cout<

}//while

}//cchild_thread

void* child_thread1(void*arg){

sleep(1);

std::cout<

pthread_t tid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

pthread_create(&tid,&attr,cchild_thread,(void*)which1);

std::cout<

}//child_thread

void* child_thread2(void*arg){

sleep(1);

std::cout<

pthread_t tid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

pthread_create(&tid,&attr,cchild_thread,(void*)which2);

int a=0;

std::cout<

pthread_exit((void*)1);

}//child_thread2

int main(int argc,char** argv){

pthread_t tid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

int a=5;

pthread_create(&tid,&attr,child_thread1,(void *)a);

pthread_create(&tid,&attr,child_thread2,(void *)a);

sleep(1);

std::cout<

std::cout<

pthread_exit((void*)1);

}

运行结果:

0818b9ca8b590ca3270a3433284dd417.png

4.pthread_exit()会清除局部变量(即栈上的变量),所以当采用这种方法等待所创建的线程,如果被等待的线程中用到了进程栈上的数据,会出现断错误。下面给出pthread_exit释放栈变量的代码:

#include

#include

#include

#include

#include

class test{

int id;

public:

test(int i):id(i){

std::cout<

}

~test(){

std::cout<

}

};

void * thread(void * argv){

for(int i=0;i<5;i++){

sleep(2);

std::cout<

}//

}//

test t1(1);

int main(int argc,char** argv){

pthread_t thid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

pid_t pid;

test t2(2);

if((pid=fork())==0){

sleep(5);

}else if(pid>0){

if((pthread_create(&thid,&attr,thread,NULL))!=0)

std::cout<

else

std::cout<

//sleep(10);

pthread_exit(0);//in main for wait

}else{

std::cout<

}

return EXIT_SUCCESS;

}

输出结果为:

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值