Linux的pthread_cleanup_push()和pthread_cleanup_pop()函数

版权声明:转载请声明 https://blog.csdn.net/qq_40732350/article/details/82715216

注册线程清理函数

void pthread_cleanup_push(void (*routine)(void *),void *arg); 

触发清理函数

void pthread_cleanup_pop(int execute);   

显然pthread_cleanup_push() 是挂接 清理函数的,它的返回值类型为 void,有两个入口参数,第一个参数是清理函数函数指针,第二个参数是传给清理函数的 typeless pointer 。

另一个兄弟 pthread_cleanup_pop() 是来触发清理函数的,是按照相反的顺序来触发清理函数的。而如果它的入口参数 execute 为0值,则对应的清理函数并没有真正的执行。

注意:

pthread_cleanup_pop() 虽然要求里面的参数不能为0,但是就算为0, 还是触发了,我就高不懂了,

在一篇博客中我看到这样的解释觉得很合理:

当pthread_cleanup_pop()函数的参数为0时,仅仅在线程调用pthread_exit函数或者其它线程对本线程调用 pthread_cancel函数时,才在弹出“清理函数”的同时执行该“清理函数”。

但是当我用下面的方式时,有时候能能调用清理函数,有时候不能调用清理函数

    pthread_cleanup_pop(0);
    return((void *)1);

因此最好最安全的方式是:

    pthread_cleanup_push(cleanup, "这里是要传的void参数");
    pthread_cleanup_pop(1);
    pthread_exit((void *)1);

线程和进程相关函数的相识总结:

实例代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <pwd.h>
#include <signal.h>
#include <setjmp.h>
void
cleanup(void *arg)
{
	printf("cleanup: %s\n", (char *)arg);
}
void *
thr_fn1(void *arg)
{
	printf("thread 1 start\n");
	pthread_cleanup_push(cleanup, "thread 1 first handler");
	pthread_cleanup_push(cleanup, "thread 1 second handler");
	printf("thread 1 push complete\n");
	if (arg)
		pthread_exit((void *)1);
	pthread_cleanup_pop(1);
	pthread_cleanup_pop(1);
	pthread_exit((void *)1);
}
void *
thr_fn2(void *arg)
{
	printf("thread 2 start\n");
	pthread_cleanup_push(cleanup, "thread 2 first handler");  //注册线程清理函数
	pthread_cleanup_push(cleanup, "thread 2 second handler");
	printf("thread 2 push complete\n");
	if (arg)
		pthread_exit((void *)2);
	pthread_cleanup_pop(1);     //触发清理函数
	pthread_cleanup_pop(1);
	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("can’t create thread 1\n");
	//注册第二个线程
	err = pthread_create(&tid2, NULL, thr_fn2, (void *)1);
	if (err != 0)
		printf("can’t create thread 2\n");
	//等待第一个线程
	err = pthread_join(tid1, &tret);
	if (err != 0)
		printf("can’t join with thread 1\n");
	printf("thread 1 exit code %ld\n", (long)tret);
	//等待第一个线程
	err = pthread_join(tid2, &tret);
	if (err != 0)
		printf("can’t join with thread 2\n");
	printf("thread 2 exit code %ld\n", (long)tret);
	
	exit(0);
}

 

运行结果:

# ./a.out 
thread 2 start
thread 2 push complete
cleanup: thread 2 second handler
cleanup: thread 2 first handler
thread 1 start
thread 1 push complete
cleanup: thread 1 second handler
cleanup: thread 1 first handler
thread 1 exit code 1
thread 2 exit code 2

猜你喜欢

转载自blog.csdn.net/qq_40732350/article/details/82715216