日期:2014-05-16  浏览次数:20549 次

linux多线程编程的两个知识点
对pthread_cleanup_push和pthread_cleanup_pop不怎么理解,大虾们帮我解释具体解释一下,如果能有个简单的例子就更好了!

------解决方案--------------------
记错了,,pthread_kill是发送信号的,,
应该是pthread_cancel.

int pthread_cancel(pthread_t thread);

When a cancellation requested is acted on, the following steps occur
for thread (in this order):

1. Cancellation clean-up handlers are popped (in the reverse of the
order in which they were pushed) and called. (See
pthread_cleanup_push(3).)

2. Thread-specific data destructors are called, in an unspecified
order. (See pthread_key_create(3).)

3. The thread is terminated. (See pthread_exit(3).)

------解决方案--------------------
探讨

记错了,,pthread_kill是发送信号的,,
应该是pthread_cancel.

int pthread_cancel(pthread_t thread);

When a cancellation requested is acted on, the following steps occur
for thread (in this order):
……

------解决方案--------------------
用用就知道了, 你去看看头文件也能明白, 它要求用户必须成对使用, 因为这是两个宏, 第一个宏是{结尾,第二个宏是}结尾, 配对使用保证程序在花括号中执行时意外退出可以给用户一个清理的机会.

比如:

{
{ 执行到这个push出错了,那么堆里就一个{,于是执行它对应的}清理函数。
}
}