Linux线程(3): 线程清理处理程序
来源:互联网 发布:手机隧道测量软件 编辑:程序博客网 时间:2024/05/29 05:54
这是线程级的清理处理, 类似于进程级的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;
]
#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
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线程(3): 线程清理处理程序
- 线程清理处理程序
- 线程清理处理程序
- linux线程退出时执行的程序(线程清理处理程序)简单例子
- Linux Advance--线程清理处理函数
- 线程的清理处理程序pthread_cleanup_(线程六)
- linux下 c中怎么让才能安全关闭线程 和 linux线程退出时执行的程序(线程清理处理程序)简单例子
- 《Unix环境高级编程》:线程清理处理程序
- 线程清理处理函数 pthread_cleanup_push、pthread_cleanup_pop
- 关闭程序后的清理线程
- 线程清除处理程序
- linux线程系列(5)线程清理和控制函数
- Linux 线程信号处理
- POSIX线程清理函数
- 线程取消与清理
- pthread线程清理
- 78-线程清理函数
- 线程清理函数
- SVG图形的两个制作工具——Inkscape和Sodipodi
- CSharp Tips:以整数的形式传递对象引用
- 【转】Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命周期以及跟踪服务
- asp.net跳转页面的三种方法比较
- 在打开有模式窗体后还能"显示"无模式窗体
- Linux线程(3): 线程清理处理程序
- js中innerHTML与innerText的用法与区别
- 【转】Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触
- B树
- com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection timed out: connect数据库
- hibernate的几种映射
- ReportServies 报表开发心得
- 多线程如何传递参数
- vb打开并重新保存EXCEL文件