C++多线程之——pthread_cleanup_push与pthread_cleanup_pop

pthread_cleanup_push 是 POSIX 线程库中的一个宏(macro),用于设置线程清理函数(thread cleanup function)。它通常与 pthread_cleanup_pop 一起使用,用于在线程退出时执行一些清理工作。这些清理工作通常包括释放分配的资源或执行必要的操作,以确保线程退出时不会留下未完成的工作或资源泄漏。

下面是 pthread_cleanup_push 的基本用法:

#include <pthread.h>

void pthread_cleanup_push(void (*routine)(void *), void *arg);
  • routine: 一个函数指针,指向要执行的清理函数。这个函数接受一个 void* 类型的参数,通常用于传递需要清理的资源或数据。

  • arg: 一个 void* 类型的参数,用于传递给清理函数,以便它在执行时能够访问到需要清理的资源或数据。

pthread_cleanup_push 宏的工作方式如下:

  1. 当线程执行到 pthread_cleanup_push 时,会将 routinearg 保存到一个线程本地的清理函数堆栈中。这表示在线程退出时,将按照相反的顺序执行清理函数。

  2. 当线程遇到线程退出点(如调用 pthread_exit、返回从线程函数中等等)时,系统会自动执行清理函数堆栈中的清理函数。清理函数会以相反的顺序执行,即最后一个压入堆栈的函数会最先执行。

下面是一个简单的示例,演示了如何使用 pthread_cleanup_pushpthread_cleanup_pop 来设置线程清理函数:

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

void cleanup_function(void *arg) {
    printf("Cleaning up: %s\n", (char *)arg);
}

void *thread_function(void *arg) {
    pthread_cleanup_push(cleanup_function, "Resource 1");
    pthread_cleanup_push(cleanup_function, "Resource 2");

    // 模拟线程执行过程中的清理
    if (arg != NULL) {
        printf("Thread function received: %s\n", (char *)arg);
    }

    pthread_cleanup_pop(1); // 弹出一个清理函数,但不执行
    pthread_cleanup_pop(1); // 弹出另一个清理函数,并执行

    return NULL;
}

int main() {
    pthread_t my_thread;

    pthread_create(&my_thread, NULL, thread_function, "Hello from main!");

    // 等待新线程完成
    pthread_join(my_thread, NULL);

    printf("Main thread: New thread has finished.\n");

    return 0;
}

在这个示例中,我们使用 pthread_cleanup_push 来设置两个清理函数,它们用于释放资源。在 thread_function 中,我们模拟了一些线程执行期间的操作,并在函数退出时执行清理函数。清理函数以相反的顺序执行,并打印出相应的清理信息。这有助于确保线程退出时资源得到适当地清理。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值