APUE编程:55---线程处理(线程退出清理函数:pthread_cleanup_push、pthread_cleanup_pop)

一、注册清理函数:pthread_cleanup_push

#include <pthread.h>
void pthread_cleanup_push(void (*rtn)(void *), void *arg);
  • 功能:向栈中注册一个线程退出清理函数
  • 参数:
    • rtn:注册函数的函数指针
    • arg:注册函数所使用的参数
  • 因为处理程序记录在栈中,所以先注册的pthread_cleanup_push函数后执行。(并且pthread_cleanup_push函数可以设置多个)
  • pthread_cleanup_push所注册的函数只有在线程执行下列动作时才会被执行(如果不是下面3种情况(例如在线程中return等),pthread_cleanup_push函数就不会执行):
    • ①调用 pthread_exit 时
    • ②响应取消请求时
    • ③用非零execute参数调用pthread_cleanup_pop函数时

二、删除/执行清理函数:pthread_cleanup_pop

#include <pthread.h>
void pthread_cleanup_pop(int execute);
  •  功能:用来删除/执行一个清理函数,具体与参数有关
  • 参数:
    • 如果参数为0:用来删除最新一次向栈内注册的线程退出清理函数(注意:调用一次只清理一个)
    • 如果参数为非0:那么就调用最新一次pthread_cleanup_push所注册的函数

pthread_cleanup_push、pthread_cleanup_pop这两个函数的限制:

  • 由于他们可以实现为宏,所以必须在与线程相同的作用域中以匹配对的形式使用。例如:pthread_cleanup_push的宏可以包含字符“{”,这种情况下,在 pthread_cleanup_pop 的定义中要有对应的匹配字符“}”

三、演示案例

#include <stdio.h>
#include <stdlib.h>
#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\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(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(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);//创建线程1
    if (err != 0) {
        printf("Can't create thread 1\n");
        exit(0);
    }
    err = pthread_create(&tid2, NULL, thr_fn2, (void *)1);//创建线程2
    if (err != 0) {
        printf("Can't create thread 2\n");
        exit(0);
    }

    err = pthread_join(tid1, &tret);//等到线程1
    if (err != 0) {
        printf("Can't join with thread 1\n");
        exit(0);
    }
    printf("thread 1 exit code: %ld\n", (long)tret);

    err = pthread_join(tid2, &tret);//等待线程2
    if (err != 0) {
        printf("Can't join with thread 2\n");
        exit(0);
    }
    printf("thread 2 exit code: %ld\n", (long)tret);

    exit(0);
}

运行结果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

董哥的黑板报

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值