您的位置:首页 > 运维架构 > Linux

Linux线程(3): 线程清理处理程序

2007-08-01 15:35 369 查看
这是线程级的清理处理, 类似于进程级的atexit, 这在前面的进程环境和进程控制系列文章中有过比较详细的介绍.

1. 触发清理程序的3个条件:

调用pthread_exit时.

响应取消请求时.

用非0的execute参数调用pthread_cleanup_pop时.

2. pthread_cleanup_push函数:

原型: void pthread_cleanup_push(void (*rtn)(void *), void *arg);

头文件: <pthread.h>

参数:

rtn为清理函数的名字.

arg为该清理函数的参数.

说明: 这是线程清理处理程序的注册函数.

3. pthread_cleanup_pop函数:

原型: void pthread_cleanup_pop(int execute);

头文件: <pthread.h>

参数: 如为0, 清理函数不被调用.

4. 实例:

main.c 代码:


#include <pthread.h>


#include <stdio.h>




void cleanup(void *arg)




...{


printf("cleanup: %s ", (char *)arg);


}




void *thr_fn1(void *arg)




...{


printf("thread 1 start ");


pthread_cleanup_push(cleanup, "thread 1 first handler");


pthread_cleanup_push(cleanup, "thread 1 second handler");


printf("thread 1 push complete ");




if (arg)


return ((void *)1);




pthread_cleanup_pop(0);


pthread_cleanup_pop(0);




return ((void *)1);


}




void *thr_fn2(void *arg)




...{


...


...


if (arg)


pthread_exit((void *)2);


...


...


pthread_exit((void *)2);


}




int main()




...{


int err;


pthread_t tid1, tid2;


void *tret;




err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);


if (err != 0)


perror("can't create thread 1");




err = pthread_create(&tid2, NULL, thr_fn1, (void *)1);


if (err != 0)


perror("can't create thread 2");




err = pthread_join(tid1, &tret);


if (err != 0)


perror("cant join with thread 1");




printf("thread 1 exit code: %d ", (int)tret);




err = pthread_join(tid2, &tret);


if (err != 0)


perror("can't join with thread 2");




printf("thread 2 exit code: %d ", (int)tret);




return 0;


]

运行结果:


thread 1 start


thread 1 push complete


thread 2 start


thread 2 push complete


cleanup: thread 2 second handler


cleanup: thread 2 first handler


thread 1 exit code: 1


thread 2 exit code: 2

由运行结果可见:

1号线程并没有执行清理处理函数, 因为它是return退出的, 而不是用pthread_exit函数退出.

线程的清理处理函数执行顺序和注册顺序相反, 这和进程的atexit清理处理函数一致.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: