Linux_5.4_线程清理
来源:互联网 发布:网络结婚誓词新郎搞笑 编辑:程序博客网 时间:2024/04/29 03:53
一般来说,Posix的线程终止有两种情况:正常终止和非正常终止。线程主动调用pthread_exit()或者从线程函数中return都将使线程正常退出,这是可预见的退出方式;非正常终止是线程在其他线程的干预下,或者由于自身运行出错(比如访问非法地址)而退出,这种退出方式是不可预见的。
不论是可预见的线程终止还是异常终止,都会存在资源释放的问题,在不考虑因运行出错而退出的前提下,如何保证线程终止时能顺利的释放掉自己所占用的资源,特别是锁资源,就是一个必须考虑解决的问题。
最经常出现的情形是资源独占锁的使用:线程为了访问临界资源而为其加上锁,但在访问过程中被外界取消,如果线程处于响应取消状态,且采用异步方式响应,或者在打开独占锁以前的运行路径上存在取消点,则该临界资源将永远处于锁定状态得不到释放。外界取消操作是不可预见的,因此的确需要一个机制来简化用于资源释放的编程。
pthread_cleanup_push 函数原型:
pthread_cleanup_push 执行压栈清理函数的操作
所需头文件
#include <pthread.h>
函数说明
执行压栈清理函数的操作
函数原型
void pthread_cleanup_push(void (*routine)(void *), void *arg)
函数传入值
routine
要加入的清理函数
arg
传给清理函数的参数
函数返回值
无
错误代码
无
pthread_cleanup_pop函数原型:
pthread_cleanup_pop 执行从栈中删除清理函数的操作
所需头文件
#include <pthread.h>
函数说明
执行从栈中删除清理函数的操作
函数原型
void pthread_cleanup_pop(int execute);
函数传入值
execute
表示执行到pthread_cleanup_pop()时是否在弹出清理函数的同时执行该函数,
为0表示不执行,非0为执行
函数返回值
无
错误代码
无
pthread_cleanup_push()/pthread_cleanup_pop()采用先入后出的栈结构管理,void routine(void *arg)函数在调用pthread_cleanup_push()时压入清理函数栈,多次对pthread_cleanup_push()的调用将在清理函数栈中形成一个函数链,在执行该函数链时按照压栈的相反顺序弹出。execute参数表示执行到pthread_cleanup_pop()时是否在弹出清理函数的同时执行该函数,为0表示不执行,非0为执行;这个参数并不影响异常终止时清理函数的执行。
pthread_cleanup_push()/pthread_cleanup_pop()是以宏方式实现的,这是pthread.h中的宏定义:
#define pthread_cleanup_push(routine,arg) { struct _pthread_cleanup_buffer _buffer; _pthread_cleanup_push (&_buffer, (routine), (arg));#define pthread_cleanup_pop(execute) _pthread_cleanup_pop (&_buffer, (execute)); }可见,pthread_cleanup_push()带有一个"{",而pthread_cleanup_pop()带有一个"}",因此这两个函数必须成对出现,且必须位于程序的同一级别的代码段中才能通过编译。在下面的例子里,当线程在"do some work"中终止时,将主动调用pthread_mutex_unlock(mut),以完成解锁动作。
work"中终止时,将主动调用pthread_mutex_unlock(mut),以完成解锁动作。pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut);pthread_mutex_lock(&mut);/* do some work */pthread_mutex_unlock(&mut);pthread_cleanup_pop(0);必须要注意的是,如果线程处于PTHREAD_CANCEL_ASYNCHRONOUS状态,上述代码段就有可能出错,因为CANCEL事件有可能在pthread_cleanup_push()和pthread_mutex_lock()之间发生,或者在pthread_mutex_unlock()和pthread_cleanup_pop()之间发生,从而导致清理函数unlock一个并没有加锁的mutex变量,造成错误。因此,在使用清理函数的时候,都应该暂时设置成PTHREAD_CANCEL_DEFERRED模式。为此,POSIX的Linux实现中还提供了一对不保证可移植的pthread_cleanup_push_defer_np()/pthread_cleanup_pop_defer_np()扩展函数,功能与以下代码段相当:{ int oldtype; pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); pthread_cleanup_push(routine, arg); ... pthread_cleanup_pop(execute); pthread_setcanceltype(oldtype, NULL); }
测试实例:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *clean(void *arg)
{
printf("cleanup :%s \n",(char *)arg);
return (void *)0;
}
void *thr_fn1(void *arg)
{
printf("thread 1 start \n");
pthread_cleanup_push( (void*)clean,"thread 1 first handler");
pthread_cleanup_push( (void*)clean,"thread 1 second hadler");
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( (void*)clean,"thread 2 first handler");
pthread_cleanup_push( (void*)clean,"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)
{
printf("error .... \n");
return -1;
}
err=pthread_create(&tid2,NULL,thr_fn2,(void *)1);
if(err!=0)
{
printf("error .... \n");
return -1;
}
err=pthread_join(tid1,&tret);
if(err!=0)
{
printf("error .... \n");
return -1;
}
printf("thread 1 exit code %d \n",(int)tret);
err=pthread_join(tid2,&tret);
if(err!=0)
{
printf("error .... ");
return -1;
}
printf("thread 2 exit code %d \n",(int)tret);
return 1;
}
- Linux_5.4_线程清理
- Linux_5.3_线程同步
- Linux_5.2_线程互斥
- Linux_5.1_多线程
- 5.4.4清理资源线程
- POSIX线程清理函数
- 线程清理处理程序
- 线程取消与清理
- 线程清理处理程序
- pthread线程清理
- 78-线程清理函数
- 线程清理函数
- 线程清理函数
- 互斥体_创建4线程_打印
- 388_定时清理缓存
- 线程终止时的清理
- 取消线程及清理工作
- 线程清理函数pthread_cleanup_push / pthread_cleanup_pop
- Tautology
- PLY格式文件详解
- LeetCode Longest Palindromic Substring
- 栈-链式存储结构-c语言
- 【日常】...
- Linux_5.4_线程清理
- Solr游标
- java实现二维码([带]logo)的绘制和解析(zxing by google)
- 串口通讯—异步通信方式
- 教程:在ZYBO中增加串口情况时如何用printf函数打印
- 使用R的igraph,竟然能大致模拟展现人脑思维
- Java JProfiler教程
- android 中 任务、进程和线程的区别
- Java中如何克隆集合——ArrayList和HashSet深拷贝