pthread_cleanup_push()/pthread_cleanup_pop()的详解
2015-05-28 16:48
405 查看
一般来说,Posix的线程终止有两种情况:正常终止和非正常终止。线程主动调用pthread_exit()或者从线程函数中return都将使线程正常退出,这是可预见的退出方式;非正常终止是线程在其他线程的干预下,或者由于自身运行出错(比如访问非法地址)而退出,这种退出方式是不可预见的。
不论是可预见的线程终止还是异常终止,都会存在资源释放的问题,在不考虑因运行出错而退出的前提下,如何保证线程终止时能顺利的释放掉自己所占用的资源,特别是锁资源,就是一个必须考虑解决的问题。
最经常出现的情形是资源独占锁的使用:线程为了访问临界资源而为其加上锁,但在访问过程中被外界取消,如果线程处于响应取消状态,且采用异步方式响应,或者在打开独占锁以前的运行路径上存在取消点,则该临界资源将永远处于锁定状态得不到释放。外界取消操作是不可预见的,因此的确需要一个机制来简化用于资源释放的编程。
在POSIX线程API中提供了一个pthread_cleanup_push()/pthread_cleanup_pop()函数对用于自动释放资源 --从pthread_cleanup_push()的调用点到pthread_cleanup_pop()之间的程序段中的终止动作(包括调用 pthread_exit()和取消点终止)都将执行pthread_cleanup_push()所指定的清理函数。API定义如下:
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中的宏定义:
补充:
在线程宿主函数中主动调用return,如果return语句包含在pthread_cleanup_push()/pthread_cleanup_pop()对中,则不会引起清理函数的执行,反而会导致segment fault。
下边给一个使用例子:
不论是可预见的线程终止还是异常终止,都会存在资源释放的问题,在不考虑因运行出错而退出的前提下,如何保证线程终止时能顺利的释放掉自己所占用的资源,特别是锁资源,就是一个必须考虑解决的问题。
最经常出现的情形是资源独占锁的使用:线程为了访问临界资源而为其加上锁,但在访问过程中被外界取消,如果线程处于响应取消状态,且采用异步方式响应,或者在打开独占锁以前的运行路径上存在取消点,则该临界资源将永远处于锁定状态得不到释放。外界取消操作是不可预见的,因此的确需要一个机制来简化用于资源释放的编程。
在POSIX线程API中提供了一个pthread_cleanup_push()/pthread_cleanup_pop()函数对用于自动释放资源 --从pthread_cleanup_push()的调用点到pthread_cleanup_pop()之间的程序段中的终止动作(包括调用 pthread_exit()和取消点终止)都将执行pthread_cleanup_push()所指定的清理函数。API定义如下:
void pthread_cleanup_push(void (*routine) (void *), void *arg) void pthread_cleanup_pop(int execute)
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); }
上面我用红色标记的部分是这两个函数的关键作用,我的理解就是: pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut); pthread_mutex_lock(&mut); /* do some work */ pthread_mutex_unlock(&mut); pthread_cleanup_pop(0); 本来do some work之后是有pthread_mutex_unlock(&mut);这句,也就是有解锁操作,但是在do some work时会出现非正常终止,那样的话,系统会根据pthread_cleanup_push中提供的函数,和参数进行解锁操作或者其他操作,以免造成死锁!
补充:
在线程宿主函数中主动调用return,如果return语句包含在pthread_cleanup_push()/pthread_cleanup_pop()对中,则不会引起清理函数的执行,反而会导致segment fault。
下边给一个使用例子:
#include<pthread.h> #include<unistd.h> #include<stdio.h> #include<string.h> #include<stdlib.h> static pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond=PTHREAD_COND_INITIALIZER; struct node { int n_number; struct node *n_next; } *head=NULL; /*[thread_func]*/ /*释放节点内存*/ static void cleanup_handler(void*arg) { printf("Clean up handler of second thread.\n"); free(arg); (void)pthread_mutex_unlock(&mtx); } static void *thread_func(void *arg) { struct node*p=NULL; pthread_cleanup_push(cleanup_handler,p); while(1) { pthread_mutex_lock(&mtx); //这个mutex_lock主要是用来保护wait等待临界时期的情况, //当在wait为放入队列时,这时,已经存在Head条件等待激活 //的条件,此时可能会漏掉这种处理 //这个while要特别说明一下,单个pthread_cond_wait功能很完善, //为何这里要有一个while(head==NULL)呢?因为pthread_cond_wait //里的线程可能会被意外唤醒,如果这个时候head==NULL, //则不是我们想要的情况。这个时候, //应该让线程继续进入pthread_cond_wait while(1) { while(head==NULL) { pthread_cond_wait(&cond,&mtx); } //pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx, //然后阻塞在等待队列里休眠,直到再次被唤醒 //(大多数情况下是等待的条件成立而被唤醒,唤醒后, //该进程会先锁定先pthread_mutex_lock(&mtx);, //再读取资源用这个流程是比较清楚的 /*block-->unlock-->wait()return-->lock*/ p=head; head=head->n_next; printf("Got%dfromfrontofqueue\n",p->n_number); free(p); } pthread_mutex_unlock(&mtx);//临界区数据操作完毕,释放互斥锁 } pthread_cleanup_pop(0); return 0; } int main(void) { pthread_t tid; int i; struct node *p; pthread_create(&tid,NULL,thread_func,NULL); //子线程会一直等待资源,类似生产者和消费者, //但是这里的消费者可以是多个消费者, //而不仅仅支持普通的单个消费者,这个模型虽然简单, //但是很强大 for(i=0;i<10;i++) { p=(struct node*)malloc(sizeof(struct node)); p->n_number=i; pthread_mutex_lock(&mtx);//需要操作head这个临界资源,先加锁, p->n_next=head; head=p; pthread_cond_signal(&cond); pthread_mutex_unlock(&mtx);//解锁 sleep(1); } printf("thread1wannaendthecancelthread2.\n"); pthread_cancel(tid); //关于pthread_cancel,有一点额外的说明,它是从外部终止子线程, //子线程会在最近的取消点,退出线程,而在我们的代码里,最近的 //取消点肯定就是pthread_cond_wait()了。 pthread_join(tid,NULL); printf("Alldone--exiting\n"); return 0; }
相关文章推荐
- pthread_cleanup_push()/pthread_cleanup_pop()的详解
- pthread_clean_push与pthread_cleanup_pop函数详解
- pthread_cleanup_push()/pthread_cleanup_pop()的详解
- pthread_cleanup_push()/pthread_cleanup_pop()的详解
- 【转】pthread_cleanup_push()/pthread_cleanup_pop()的详解
- ZT pthread_cleanup_push()/pthread_cleanup_pop()的详解
- pthread_cleanup_push()/pthread_cleanup_pop()详解
- pthread_cleanup_push()/pthread_cleanup_pop()的详解
- pthread_cleanup_push()/pthread_cleanup_pop()的详解
- pthread_cleanup_push与pthread_cleanup_pop的目的(作用)
- linux高级编程之线程间的通信(pthread_cleanup_push和pthread_cleanup_pop)
- pthread_testcancel,pthread_cleanup_push与pthread_cleanup_pop的作用
- pthread_cleanup_push与pthread_cleanup_pop的目的(作用)是什么
- pthread_cleanup_push与pthread_cleanup_pop的目的 作用
- pthread_cleanup_push()和pthread_cleanup_pop()的理解
- pthread_cleanup_push 和 pthread_cleanup_pop
- unix 线程回调函数使用-pthread_cleanup_push && pthread_cleanup_pop
- pthread_cleanup_push与pthread_cleanup_pop与pthread_cancel与pthread_testcancel
- 线程清理处理函数 pthread_cleanup_push、pthread_cleanup_pop
- pthread_cleanup_push与pthread_cleanup_pop的目的 作用