Linux 线程控制 —— 线程清理 pthread_cleanup_push

主线程可以通道 pthread_cancel 主动终止子线程,但是子线程中可能还有未被释放的资源,比如malloc开辟的空间。如果不清理,很有可能会造成内存泄漏。

// 子线程回调函数
void* thread_run(void* args)
{
    int* p = (int*)malloc(100);      // 动态开辟内存
    pthread_testcancel();            // 设置取消点

    free(p);
    p = NULL;
    // ...
}

因此,为了避免这种情况,于是就有了一对线程清理函数 pthread_cleanup_push 和 pthread_cleanup_pop 。两者必须是成对存在的,否则无法编译通过


        目录

1、pthread_cleanup_push

2、pthread_cleanup_pop

3、实际应用

(1) 子线程主动退出 (pthread_exit)

(2) 子线程被主线程取消 (pthread_cancel)

(3) pthread_cleanup_pop的参数为非0值 


1、pthread_cleanup_push

pthread_cleanup_push 的作用是创建栈帧,设置回调函数,该过程相当于入栈。回调函数的执行与否有以下三种情况:

  • 线程被取消的时候(pthread_cancel)
  • 线程主动退出的时候(pthread_exit)
  • pthread_cleanup_pop的参数为非0值(pthread_cleanup_pop)

第一个参数 routine:回调清理函数。当上面三种情况的任意一种存在时,回调函数就会被调用

第二个参数 args:要传递个回调函数的参数

2、pthread_cleanup_pop

pthread_cleanup_pop 函数的作用是执行回调函数 或者 销毁栈帧,该过程相当于出栈。根据传入参数的不同执行的结果也会不同。

当 execute = 0 时, 处在栈顶的栈帧会被销毁,pthread_cleanup_push的回调函数不会被执行

当 execute != 0 时,pthread_cleanup_push 的回调函数会被执行。

3、实际应用

pthread_cleanup_push 和pthread_cleanup_pop 必须是成对存在的,否则无法编译通过。

(1) 子线程主动退出 (pthread_exit)

这里 pthread_cleanup_pop 函数的放置位置和参数需要注意:

  • 必须放在 pthread_exit 后面,否则pthread_cleanup_pop会先清除栈帧,pthread_exit就无法调用清理函数了。
  • pthread_cleanup_pop的参数是 0,因为pthread_cleanup_pop的参数为非0值时也会调用回调清理函数。
void* pthread_cleanup(void* args){
	printf("线程清理函数被调用了\n");
}

void* pthread_run(void* args)
{
	pthread_cleanup_push(pthread_cleanup, NULL);

	pthread_exit((void*)1);     // 子线程主动退出
	pthread_cleanup_pop(0);     // 这里的参数要为0,否则回调函数会被重复调用
}

int main(){
	pthread_t tid;
	pthread_create(&tid, NULL, pthread_run, NULL);
	sleep(1);

	pthread_join(tid, NULL);
	return 0;
}

(2) 子线程被主线程取消 (pthread_cancel)

这里 pthread_cleanup_pop 的放置位置和参数 同上。

void* pthread_cleanup(void* args){
	printf("线程清理函数被调用了\n");
}

void* pthread_run(void* args)
{
	pthread_cleanup_push(pthread_cleanup, NULL);

	pthread_testcancel();       // 设置取消点
	pthread_cleanup_pop(0);     // 这里的参数要为0,否则回调函数会被重复调用
}

int main(){
	pthread_t tid;
	pthread_create(&tid, NULL, pthread_run, NULL);
	pthread_cancel(tid);        // 取消线程
	sleep(1);

	pthread_join(tid, NULL);
	return 0;
}

(3) pthread_cleanup_pop的参数为非0值 

void* pthread_cleanup(void* args){
	printf("线程清理函数被调用了\n");
}

void* pthread_run(void* args)
{
	pthread_cleanup_push(pthread_cleanup, NULL);

	pthread_cleanup_pop(1);     // 这里的参数为非0值
}

int main(){
	pthread_t tid;
	pthread_create(&tid, NULL, pthread_run, NULL);
	sleep(1);

	pthread_join(tid, NULL);
	return 0;
}

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

余额充值