Linux系统线程用完,Linux系统编程—线程清理

有时候我们希望线程退出时能够自动的执行某些函数,为了能达到此目的,OS 提供了两个函数帮我们完成这个功能:

void pthread_cleanup_push(void (*rtn)(void*), void *arg);

void pthread_cleanup_pop(int execute);

使用方法

如果想要你的线程在退出时能够执行清理函数,你需要使用 pthread_cleanup_push 对你的清理函数进行注册,如下:

void clean(void *arg) {

// ...

}

void *th_fn(void *arg) {

// push 和 pop 必须对对出现

pthread_cleanup_push(clean, /* 清理函数 clean 的参数*/);

// ...

pthread_cleanup_pop(1);

}

在 Linux 中,pthread_cleanup_push 和 pthread_cleanup_pop 这两个函数是通过宏来做的,pthread_cleanup_push 被替换成以左花括号 { 为开头的一段代码,而 pthread_cleanup_pop 被替换成以右花括号 } 结尾的一段代码,这就意味着这两个函数必须要成对出现才能将左右花括号匹配上,否则就出现编译错误。

有些平台可能不是使用宏来实现,就算不成对也没什么关系。

线程清理函数调用

有三种情况线程清理函数会被调用:

1、线程还未执行 pthread_cleanup_pop 前,被 pthread_cancel 取消

2、线程还未执行 pthread_cleanup_pop 前,主动执行 pthread_exit 终止

3、线程执行 pthread_cleanup_pop,且 pthread_cleanup_pop 的参数不为 0

注意:如果线程还未执行 pthread_cleanup_pop 前通过 return 返回,是不会执行清理函数的。

线程清理例程

程序 clean 需要传入两个参数,第 1 个参数表示是否提前返回(在执行 pthread_cleanup_pop 前返回),第 2 个参数表示 pthread_cleanup_pop 的参数。所以有 4 种组合情况。

#include

#include

#include

#include

int excute;

void cleanup(void* arg) {

printf("cleanup: %s\n", (char*)arg);

}

void* th_fn1(void* arg) {

puts("thread 1 starting");

pthread_cleanup_push(cleanup, "线程 1 清理者 1 号");

pthread_cleanup_push(cleanup, "线程 1 清理者 2 号");

if (arg) {

printf("线程 1 提前退出\n");

return (void*)1;

}

pthread_cleanup_pop(excute);

pthread_cleanup_pop(excute);

printf("线程 1 正常退出\n");

return (void*)10;

}

void* th_fn2(void* arg) {

puts("thread 2 starting");

pthread_cleanup_push(cleanup, "线程 2 清理者 1 号");

pthread_cleanup_push(cleanup, "线程 2 清理者 2 号");

if (arg) {

printf("线程 2 提前退出\n");

pthread_exit((void*)2);

}

pthread_cleanup_pop(excute);

pthread_cleanup_pop(excute);

printf("线程 2 正常退出\n");

pthread_exit((void*)20);

}

int main(int argc, char* argv[]) {

if (argc < 3) {

printf("Usage: %s \n", argv[0]);

return -1;

}

pthread_t tid1, tid2;

int err;

void* ret;

void *arg = NULL;

excute = 0;

arg = (void*)atoi(argv[1]);

excute = atoi(argv[2]);

err = pthread_create(&tid1, NULL, th_fn1, arg);

err = pthread_create(&tid2, NULL, th_fn2, arg);

err = pthread_join(tid1, &ret);

printf("thread 1 exit code %d\n", (int)ret);

err = pthread_join(tid2, &ret);

printf("thread 2 exit code %d\n", (int)ret);

return 0;

}

7371bbd3c9b7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

结果可以看到:

当 clean 程序中的线程正常返回时,只有 pthread_cleanup_pop 的参数非 0 时,才会正常执行清理函数。

当 clean 程序中的线程在执行 pthread_cleanup_pop 前时,使用 pthread_exit 退出时,清理函数才会被执行,和pthread_cleanup_pop 的参数没有关系。而使用 return 返回的线程 1 并不会执行清理函数。清理函数的执行顺序,是按照注册时候相反的顺序执行的。

注意,在有些系统中(如Mac OS X),提前终止可能会出现段错误。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值