linux pthread_cleanup_push 线程实现,关于 pthread_cleanup_push 的问题

关于 pthread_cleanup_push 有如下描述:

================================

从pthread_cleanup_push()的调用点到pthread_cleanup_pop()之间的程序段中的终止动作

(包括调用pthread_exit()和取消点终止)都将执行pthread_cleanup_push()所指定的清理函数。

================================

请问其中的“取消点终止”是什么意思?

我尝试了以下的 pthread_cancel(),但并不会触发 clean(),这是为什么?

以下是代码:

#include

#include

#include

#include

void clean(void* p)

{

printf("clean()\n");

}

void* thread(void* p)

{

pthread_cleanup_push(clean, NULL);

//===============================

printf("thread sleep\n");

sleep(10);

printf("thread wake...\n");

//===============================

pthread_cleanup_pop(1);

}

int main()

{

pthread_t        id;

pthread_create(&id, NULL, thread, NULL);

printf("main sleep\n");

sleep(3);

printf("cancel thread: %u\n", id);

pthread_cancel(id);

return 0;

}

==========================

输出:

thread sleep

main sleep

cancel thread: 3085872016

==========================

在 xxx_push() 和 xxx_pop() 之间唯有调用 pthread_exit() 会触发 clean() 函数,

return 也不会,(异常没有试过, 还不会手动抛出异常);

这是一个老问题了,也看到别人问过(比如: http://bbs.chinaunix.net/viewthread.php?tid=456153)

但在网上一直没有找到正式的回答;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值