pthread_cleanup_push/pop成对出现的意义

首先简单介绍一下这两条API

线程可以安排它退出时需要调用的函数,这样的函数称为线程清理处理程序,线程可以建立多个清理处理程序。处理程序记录在栈中,也就是说它们的执行顺序与它们注册时的顺序相反。

    pthread_cleanup_push来注册清理函数rtn,这个函数有一个参数arg。在以下三种情形之一发生时,注册的清理函数被执行:
    1)调用pthread_exit。
    2)作为对取消线程请求(pthread_cancel)的响应。
    3)以非0参数调用pthread_cleanup_pop。
注意:

    1)如果线程只是由于简单的返回而终止的,则清除函数不会被调用。

    2)如果pthread_cleanup_pop被传递0参数,则清除函数不会被调用,但是会清除处于栈顶的清理函数

以下需要注意的

1,push与pop一定是成对出现的,其实push中包含"{"而pop中包含"}",少一个不行。
2,push可以有多个,同样的pop也要对应的数量,遵循"先进后出原则"。

但是下面的代码经常会出现

void child(void *t)
{
  pthread_cleanup_push(pthread_mutex_unlock,&mutex);
  pthread_mutex_lock(&mutex);
  ..............
  pthread_mutex_unlock(&mutex);
  pthread_cleanup_pop(0);
}


可能大家会觉得奇怪,既然pthread_cleanup_pop(0);   的参数是0,pthread_mutex_unlock不会被调用,那么为什么还需要写pthread_cleanup_pop
可能大家会答因为push和pop是宏并且被设计成"{"和“}”,所以它们一定要一对出现。我想说的是为什么它们被设计成一对,这样子意义是什么?而且很多时候出现pthread_cleanup_pop(0),感觉这样子带来很多没有必要的麻烦(多写这个函数)。

答案是,它们成对出现时有意义,我先看下面的例子

void child(void *t)
{
  pthread_cleanup_push(pthread_mutex_unlock,&mutex);
  pthread_mutex_lock(&mutex);
  ..............
  pthread_mutex_unlock(&mutex);
  pthread_cleanup_pop(0);
  // something unimportant code
  do_something1();
  do_something2();
}

在do_something1()后面的代码,无论线程怎么退出,都没有thread_cleanup_handler被调用,所以push和pop成对出现的意义就是对thread_cleanup_handler作用域限定一定范围的代码,这样子就可以根据不同代码段注册不同的thread_cleanup_handler,当该代码段运行结束,对应的thread_cleanup_handler也被pop出来,也就是失效了。比如下面的例子
void child(void *t)
{
  pthread_cleanup_push(pthread_dosomething1_handler,&mutex);
  dosomething1();
  pthread_cleanup_pop(0);
  
  pthread_cleanup_push(pthread_dosomething2_handler,&mutex);
  dosomething2();
  pthread_cleanup_pop(0);

  pthread_cleanup_push(pthread_dosomething2_handler,&mutex);
  dosomething2();
  pthread_cleanup_pop(0);
}








  

























  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`pthread_cleanup_push` 和 `pthread_cleanup_pop` 是 POSIX 线程库提供的两个函数,用于在线程退出时清理资源。 具体来说,当线程执行到 `pthread_cleanup_push` 函数时,它会将一个清理函数和一个参数压入线程的清理栈中。当线程退出时,无论是通过线程函数的 return 语句、pthread_exit 函数还是被取消,都会自动调用清理栈中的每个清理函数,并按照压入栈的顺序依次执行。 在多线程编程中,线程可能会因为各种原因(如出现异常)而异常终止,导致没有机会清理资源,从而造资源泄漏或者其他问题。使用 `pthread_cleanup_push` 和 `pthread_cleanup_pop` 可以保证线程退出时一定会执行清理函数,避免这些问题。 下面是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> void cleanup_handler(void *arg) { printf("cleanup: %s\n", (char *) arg); } void *thread_func(void *arg) { char *msg = (char *) arg; printf("thread: %s\n", msg); // 压入清理函数 pthread_cleanup_push(cleanup_handler, "thread is finished"); // 执行任务 sleep(5); // 弹出清理函数 pthread_cleanup_pop(1); pthread_exit(NULL); } int main() { pthread_t tid; int ret; ret = pthread_create(&tid, NULL, thread_func, "hello world"); if (ret != 0) { fprintf(stderr, "pthread_create error\n"); exit(EXIT_FAILURE); } // 等待线程退出 ret = pthread_join(tid, NULL); if (ret != 0) { fprintf(stderr, "pthread_join error\n"); exit(EXIT_FAILURE); } printf("main: thread is finished\n"); return 0; } ``` 在这个示例中,线程执行到 `pthread_cleanup_push` 函数时,它会将 `cleanup_handler` 函数和字符串 `"thread is finished"` 压入清理栈中。当线程执行完任务后,无论是正常退出还是被取消,都会自动调用 `cleanup_handler` 函数,并打印出 `"cleanup: thread is finished"` 的消息。 需要注意的是,如果在 `pthread_cleanup_push` 和 `pthread_cleanup_pop` 之间调用了 `pthread_exit`,那么清理函数也会被执行。但是如果在 `pthread_cleanup_push` 和 `pthread_cleanup_pop` 之间调用了 `longjmp`,那么清理函数就不会被执行。因此,不要在使用 `setjmp` 和 `longjmp` 的代码中使用 `pthread_cleanup_push` 和 `pthread_cleanup_pop`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值