android linux pthread_cancel,线程取消pthread_cancel

本文详细探讨了POSIX标准下的线程取消机制,包括如何发送Cancel信号、目标线程如何响应,以及设置不同取消动作类型的函数pthread_setcanceltype。同时通过示例代码展示了取消点的概念及其在实际编程中的应用。

线程取消的语义是向目标线程发Cancel信号,但如何处理Cancel信号则由目标线程自己决定,或者忽略、或者立即终止、或者继续运行至Cancelation-point(取消点),由不同的Cancelation状态决定。根据POSIX标准,pthread_join()、pthread_testcancel()、pthread_cond_wait()、pthread_cond_timedwait()、sem_wait()、sigwait()等函数以及read()、write()等会引起阻塞的系统调用都是Cancelation-point,而其他pthread函数都不会引起Cancelation动作。

intpthread_setcanceltype(int

type, int *oldtype)

设置本线程取消动作的执行时机,type由两种取值:PTHREAD_CANCEL_DEFFERED和PTHREAD_CANCEL_ASYNCHRONOUS,仅当Cancel状态为Enable时有效,分别表示收到信号后继续运行至下一个取消点再退出和立即执行取消动作(退出);oldtype如果不为NULL则存入运来的取消动作类型值。

#include

#include

#include

void* threadFunc(void*

args)

{

pthread_setcanceltype(PTHREAD_CANCEL_ASYCHRONOUS);

while (1);

return NULL;

}

int main(void)

{

pthread_t tid;

pthread_setcancelstate();

if (pthread_create(&tid, NULL,

threadFunc, NULL))

{

printf("Error Creating

Thread.\n");

return 1;

}

sleep(3);

pthread_cancel(tid);

pthread_join(tid, NULL);

return 0;

}

pthread_cancel 与exception貌似毫无关联,实际上,In NPTL thread cancellation is implemented using

exceptions. This does not in general conflict with the mixed use of

cancellation and exceptions in C++ programs.The problem is in

functiontf.

This function contains a catch-all clause which does not rethrow

the exception. This is possible to expect but should really never

happen in any code. The rules C++ experts developed state that

catch-all cases must rethrow. If not then strange things can happen

since one doesn't always know exactly what exceptions are

thrown.The exception used for cancellation is special, it

cannot be ignored.

#include

#include

#include

static pthread_mutex_t m =

PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t c =

PTHREAD_COND_INITIALIZER;

static void*

tf(void*)

{

try

{

::pthread_mutex_lock(&m);

::pthread_cond_wait(&c, &m);

}

catch (...)

{

// do something

}

}

int main()

{

pthread_t th;

::pthread_create(&th, NULL, tf, NULL);

std::cout << "Wait a bit"

<< std::endl;

sleep(1);

::pthread_cancel(th);

::pthread_join(th, NULL);

}

But this code might not have the expected

semantics. Therefore the more general solution is to change the

code as such:

@@ -1,6 +1,7 @@

#include

#include

#include

+#include

static pthread_mutex_t m =

PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t c =

PTHREAD_COND_INITIALIZER;

@@ -11,6 +12,8 @@

try {

::pthread_mutex_lock(&m);

::pthread_cond_wait (&c, &m);

+ } catch (abi::__forced_unwind&) {

+ throw;

}

catch (...) {

// do something

}

pthread_testcancelpthread_cancel 是多线程编程中用于线程交互的两个重要函数,它们可以协调多个线程的执行,实现线程的安全退出和资源释放。 下面是 pthread_testcancelpthread_cancel线程交互模型: 1. 主线程创建一个新线程,并向新线程发送取消请求,同时将新线程取消状态设置为 PTHREAD_CANCEL_ENABLE,以确保新线程可以响应取消请求。 2. 新线程在执行任务时,不断使用 pthread_testcancel 函数检测是否收到取消请求,如果收到了取消请求,就立即退出线程函数。 3. 如果新线程正在阻塞等待或者 sleep 等待,那么就会立即响应取消请求,退出阻塞状态或者 sleep 等待,然后立即退出线程函数。 4. 如果新线程没有收到取消请求,就会继续执行任务,直到任务完成或者收到取消请求为止。 5. 如果主线程需要取消线程的执行,可以调用 pthread_cancel 函数向新线程发送取消请求,让新线程在适当的时候退出执行。 6. 如果新线程没有及时响应取消请求,那么就会出现线程无法退出的情况,从而导致资源泄露和程序异常等问题。 7. 在编写多线程程序时,需要注意线程安全性和取消操作的使用,避免竞争条件和死锁等问题,确保程序能够正确安全地运行。 总之,pthread_testcancelpthread_cancel 是多线程编程中用于线程交互的两个重要函数,它们可以协调多个线程的执行,实现线程的安全退出和资源释放。在多线程编程中,需要注意线程安全性和取消操作的使用,避免竞争条件和死锁等问题,确保程序能够正确安全地运行。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值