Linux 线程控制 —— 线程清理 pthread_cleanup_push
创始人
2024-03-13 12:24:47
0

主线程可以通道 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;
}

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...