linux进阶45——多线程程序设计(五):线程清除

线程清除压栈

函数定义

#include <pthread.h>

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

将清除函数压入清除栈。

返回值

参数

rtn:清除函数

arg:清除函数的参数

线程清除出栈

函数定义

#include <pthread.h>

void pthread_cleanup_pop(int execute);

将清除函数弹出清除栈。

返回值

参数

execute执行到pthread_cleanup_pop()时是否在弹出清理函数的同时执行该函数,非0:执行,0:不执行

示例

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *clean(void *arg)
{
	printf("cleanup :%s \n", (char*)arg);
	return (void*)0;
}

void *thr_func1(void *arg)
{
	printf("thread 1 start!\n");
	pthread_cleanup_push((void*)clean, "thread 1 first handler");
	pthread_cleanup_push((void*)clean, "thread 1 second handler");
	printf("thread 1 push complete!\n");
	
	if(arg != NULL)
	{
		return ((void *)1);
	}

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

void *thr_func2(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 != NULL)
    {
        pthread_exit ((void *)2);
    }

    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    return (void *)2;
}

int main(void)
{
	int ret;
	pthread_t tid1, tid2;
	void *tret;

	ret = pthread_create(&tid1, NULL, thr_func1, (void *)1);
	if(ret != 0)
	{
		printf("thread1 create error!\n");
		return -1;
	}
	
	ret = pthread_create(&tid2, NULL, thr_func2, (void *)1);
	if(ret != 0)
	{
		printf("thread2 create error!\n");
		return -1;
	}

	ret = pthread_join(tid1, &tret);
	if(ret != 0)
	{
		printf("thread1 join error!\n");
		return -1;		
	}
	printf("thread1 exit code is %d\n", (int)tret);
	
	ret = pthread_join(tid2, &tret);
    if(ret != 0)
    {
        printf("thread2 join error!\n");
        return -1;
    }
    printf("thread2 exit code is %d\n", (int)tret);

	return 0;
}

编译运行:

[root@192 pthread]# gcc -lpthread pthread_clean.c -o pthread_clean
pthread_clean.c: 在函数‘main’中:
pthread_clean.c:71:38: 警告:将一个指针转换为大小不同的整数 [-Wpointer-to-int-cast]
   71 |  printf("thread1 exit code is %d\n", (int)tret);
      |                                      ^
pthread_clean.c:79:45: 警告:将一个指针转换为大小不同的整数 [-Wpointer-to-int-cast]
   79 |         printf("thread2 exit code is %d\n", (int)tret);
      |                                             ^
[root@192 pthread]# ./pthread_clean
thread 2 start!
thread 2 push complete!
thread 1 start!
thread 1 push complete!
thread1 exit code is 1
cleanup :thread 2 second handler 
cleanup :thread 2 first handler 
thread2 exit code is 2
[root@192 pthread]# 

说明:

从pthread_cleanup_push的调用点到pthread_cleanup_pop之间的线程段中的终止动作(包括调用pthread_exit()和异常终止,不包括return)都将执行pthread_cleanup_push()所指定的清理函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值