多线程设计之线程清理pthread_cleanup

线程清理:pthread_cleanup_push() &  pthread_cleanup_pop()

 

A cancellation clean-up handler is poppedfrom the stack and executed in

The following circumstances:(三种情况会执行线程清理)

1.        When a thread is canceled, allof the stacked clean-up handlers are popped and executed in the reverse of theorder in which they were pushed onto the stack. (线程取消)

2.        When a thread terminates bycallingpthread_exit(3),all clean-up handlers are executed as described in the preceding point.  (Clean-up handlers are not called if thethread terminates by performing a return from the thread start function.) (pushpop之间执行了pthread_exit)

3.        When a thread calls pthread_cleanup_pop()with a nonzero execute argument, the top-most clean-up handler is popped andexecuted.(pop内为非零参数)

 

示例代码:

/*

 *pthread_push_pop.c

 *

 * Created on: Jan 21, 2013

 *     Author: linuxdba@qq.com

 */

 

#include <stdio.h>

#include <pthread.h>

 

void *clean1(void *arg)

{

         printf("clean1func: %s\n", arg);

         return(void *)0;

}

 

void *clean2(void *arg)

{

         printf("clean2func: %s\n", arg);

         return(void *)0;

}

 

void *thread(void )

{

         pthread_tthid;

         thid= pthread_self();                //获取线程ID

         printf("thethread is: %d.\n",thid);

         printf("theprocess id is: %d.\n", getpid());     //进程ID

         pthread_cleanup_push((void*)clean1,"first push");

         pthread_cleanup_push((void*)clean2, "second push");

         printf("Inthread.\n");

         pthread_exit("threadexit.\n");       //退出线程,会执行push指定函数

         /*return  NULL; */       //不会执行push指定函数

         pthread_cleanup_pop(0);//0,只有在异常退出时才会执行push指定的函数,pop第一个

         pthread_cleanup_pop(1);//为非0,即便是正常,也会执行push指定的函数,pop第二个

         returnNULL;

}

 

int main(int argc, char argv[])

{

         intret = 0;

         pthread_tpthid;

         ret= pthread_create(&pthid, NULL,(void *)thread,NULL);

         if(ret)

         {

                   printf("createthread failed.\n");

                   return-1;

         }

         printf("theprocess is: %d.\n", getpid());         //获取进程ID

         pthread_join(pthid,NULL);

 

}

 

运行结果:

the process is: 3893.

the thread is:1738999552.

the process id is: 3893.

In thread.

clean2 func: second push

clean1 func: first push

 

pushpop采用栈方式,先进后出,后进先出,所以clean2会在前面。

 

需要注意的几点:

1)        线程属于进程的,只有一个进程ID,一样的;

2)        pushpop是采用栈方式;

3)        pthread_cleanup_pop后面为0则执行push指定函数,非0则即便正常也执行

4)        pushpop之间代码若异常退出,包括pthread_exit,会进行相应的函数处理,但是若其间执行了return,则直接结束线程,不执行push指定函数。

 

参考文档:

http://man7.org/linux/man-pages/man3/pthread_cleanup_push.3.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值