现在的位置: 首页 > 综合 > 正文

pthread_cleanup_push() pthread_cleanup_pop()

2013年09月08日 ⁄ 综合 ⁄ 共 1673字 ⁄ 字号 评论关闭

void pthread_cleanup_push(void (*rtn)(void *), void *arg);注册一个线程终止时的清理函数 (类似atexit),后注册的先执行。
void pthread_cleanup_pop(int execute); 解除注册最近一个注册的线程清理函数,即线程清理函数使用类似栈的压 (push)、弹(pop)方式进行注册和解除;参数 execute 为0 时表示仅解除注册,而不执行,非 0 则表示执行该清理函数并解除注册;
要特别注意的是,pthread_cleanup_push (3)和pthread_cleanup_pop (3)可能是通过宏来实现的,必须 在同一个缩进级别的代码块中配对使用,即编写代码时不能只调用其中一个,否则编译将通不过。因为它们的宏实现通常使用了单的花括号。
(PS:这时,线程的结束方式应该为pthread_exit(),而不是return。
更不是exit(),_exit(),abort()这些是进程的结束方式,会导致整个进程结束)

#include "apue.h" // http://www.apuebook.com/ 网站上找这个头文件
#include <pthread.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:%d\n",arg);
	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(cleanup, "thread 2 first handler");
	pthread_cleanup_push(cleanup, "thread 2 second handler");
	printf("thread 2 push complete: %d\n",arg);
	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)
		err_quit("can't create thread 1: %s\n", strerror(err));
	err = pthread_create(&tid2, NULL, thr_fn2, (void *)1);
	if (err != 0)
		err_quit("can't create thread 2: %s\n", strerror(err));
	err = pthread_join(tid1, &tret);
	if (err != 0)
		err_quit("can't join with thread 1: %s\n", strerror(err));
	printf("thread 1 exit code %d\n", (int)tret);
	err = pthread_join(tid2, &tret);
	if (err != 0)
		err_quit("can't join with thread 2: %s\n", strerror(err));
	printf("thread 2 exit code %d\n", (int)tret);
	exit(0);
}

【上篇】
【下篇】

抱歉!评论已关闭.