linux线程——pthread_cleanup_push(),pthread_cleanup_pop()

本文介绍了Linux线程中的清理处理程序,包括pthread_cleanup_push和pthread_cleanup_pop的使用。pthread_cleanup_push用于注册清理函数,而pthread_cleanup_pop决定是否执行这些函数。线程在特定条件下调用pthread_exit或响应取消请求时,清理函数会被执行。需要注意的是,线程返回时,其清理处理程序不会被调用,且执行顺序与注册顺序相反。此外,还讨论了线程分离状态的设置和影响。
摘要由CSDN通过智能技术生成

线程可以安排它退出时需要调用的函数,这样的函数称为线程清理处理程序,线程可以建立多个清理处理程序。处理程序记录在栈中,也就是说它们的执行顺序与它们注册时的顺序相反。

一、pthread_cleanup_push

函数简介:

        头文件:#include <pthread.h>

        函数定义:voidpthread_cleanup_push(void (*rtn)(void*),void *arg);

        描述:

                 使用这个函数来注册清理函数rtn:这个注册函数又一个参数arg。

                 rtn处理程序入口地址

                 arg传递给处理函数的参数


注册的清理函数被执行的条件:

1)线程直接调用pthread_exit()

2)响应取消请求。(线程可以通过调用pthread_cancel函数来请求取消同一个进程中的其他线程)

3)以非0参数调用pthread_cleanup_pop。


注意

1)如果线程只是由于简单的返回而终止,此时清理函数不会被调用。return语句

2)如果pthread_cleanup_pop被传递0参数,则清理函数不会被调用,但是会清除处于栈顶的清理函数。

二、pthread_cleanup_pop

       函数定义: void  pthread_cleanup_pop(int execute);

       这里的int参数,0是不执行push的内容,非0是执行这里关键是pthread_cleanup_pop参数问题,其实int那是因为c没有bool,这里的参数只有0与非0的区别,对pthread_cleanup_pop,参数是5和10都是一样的,都是非0。


三、代码实例:


#include "apue.h"
#include <pthread.h>
void cleanup(void *arg)
{
	printf("cleanup: %s\n",(char*)arg);
}

void* thr_fn1(void *arg)
{
	printf("thread 1 start\n");
	pthread_cleanup_push(cleanup,"thread 1 first handler");
	pthread_cleanup_push(cleanup,"thread 1 second handler");
	printf("thread 1 push complete\n");
	if(arg)
	  return ((void*)1);
	pthread_cleanup_pop(0);
	pthread_cleanup_pop(0);
	return ((void*)1);
}
void* thr_fn2(void *arg)
{
	printf("thread 2 start\n");
	pthread_cleanup_push(cleanup,"thread 2 first handler");
	pthread_cleanup_push(cleanup,"thread 2 second handler");
	printf("thread 2 push complete\n");
	if(arg)
	  pthread_exit((void*)2);
	pthread_cleanup_pop(0);
	pthread_cleanup_pop(0);
	pthread_exit((void*)2);
}

int main(void)
{
	int err;
	pthread_t tid1,tid2;
	void *tret;
	err=pthread_create(&tid1,NULL,thr_fn1,(void*)1);
	if(err!=0)
	  err_quit("can't create thread 1: %s\n",strerror(err));
	
	err=pthread_create(&tid2,NULL,thr_fn2,(void*)1);
	if(err!=0)
	  err_quit("can't create thread 1: %s\n",strerror(err));

	err=pthread_join(tid1,&tret);
	if(err!=0)
	  err_quit("can't join with thread 1 : %s\n",strerror(err));
	printf("thread 1 exit cood %d\n",(int)tret);
	
	err=pthread_join(tid2,&tret);
	if(err!=0)
	  err_quit("can't join with thread 2 : %s\n",strerror(err));
	printf("thread 2 exit cood %d\n",(int)tret);
	exit(0);
}
fengpeng@ubuntu:~/workspace/work1$ gcc helloworld.c -pthread -o test
fengpeng@ubuntu:~/workspace/work1$ ./test
thread 2 start
thread 2 push complete
cleanup: thread 2 second handler
cleanup: thread 2 first handler
thread 1 start
thread 1 push complete
thread 1 exit cood 1

thread 2 exit cood 2


从输出结果来看,两个线程都正确的启动和推出了,但是只调用了第二个线程的清理处理程序,所以如果线程是通过从它的启动程中返回而终止的话,那么它的清理处理程序就不会被调用,还要注意清理处理程序是按照他们push时候相反的顺序被调用的(栈)。


四、pthread_detach线程分离状态

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值