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
    评论
引用\[1\]:在示例代码,使用了pthread_create函数创建了一个新的线程,并在新线程执行了new_thread_start函数。在线程,使用pthread_join函数等待新线程的结束,并获取新线程的返回值。最后,线程打印出新线程的终止代码。\[1\] 引用\[2\]:在另一个示例代码,同样使用了pthread_create函数创建了一个新的线程,并在新线程执行了new_thread_start函数。不同的是,线程没有使用pthread_join函数等待新线程的结束,而是直接退出。\[2\] 引用\[3\]:pthread_join函数用于等待指定的线程结束,并获取线程的返回值。它的第一个参数是需要等待的线程的ID号,第二个参数是一个指向指针的指针,用于存储线程的返回值。如果不关心线程的返回值,可以将第二个参数设置为NULL。pthread_exit函数用于退出当前线程,并返回一个指定的值。\[3\] 综上所述,pthread_exit函数用于退出线程,而pthread_join函数用于等待线程的结束并获取线程的返回值。如果需要传递整型数据,可以将整型数据作为参数传递给新线程的函数,并在新线程进行相应的处理。 #### 引用[.reference_title] - *1* *2* [Linux线程(2)——创建、终止和回收(pthread_create()、pthread_exit()、pthread_join())](https://blog.csdn.net/cj_lsk/article/details/130229709)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [系统编程05-线程(pthread_create、pthread_join、pthread_exit)](https://blog.csdn.net/weixin_48102054/article/details/127331663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值