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清理处理函数一致.
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清理处理函数一致.
相关文章推荐
- linux线程退出时执行的程序(线程清理处理程序)简单例子
- linux下 c中怎么让才能安全关闭线程 和 linux线程退出时执行的程序(线程清理处理程序)简单例子
- 线程清理处理程序
- 线程清理处理程序
- 《Unix环境高级编程》:线程清理处理程序
- APUE2勘误-11.5节 线程终止(关于线程清理处理程序)
- Linux下编译《UNIX环境高级编程》的第一个程序时出错的处理方法
- 查看Linux进程CPU过高具体的线程堆栈(不中断程序)
- Linux - 进程、线程、程序之间的区别与联系
- Linux下C程序命令行参数处理
- 这是程序启动界面,感觉处理的不错,用软件加载信息的时间作为新线程等待时间
- linux后端服务程序之信号处理
- Linux系统内核接收以太帧的处理程序
- Linux练习(处理程序命令行参数)
- 编写Linux定时处理程序
- Linux&nbsp;下&nbsp;C++程序的异常处理技巧
- 处理日常事务的 Linux 程序和它们的配置
- Linux 使用atexit注册终止处理程序
- Linux平台php命令行程序处理管道数据的方法
- 命令前加./ ,在后台运行程序 linux批处理 linux自动运行程序