C语言设置线程的取消类型和状态

1.相关函数介绍
a.

int pthread_cancel(pthread_t thread)

发送终止信号给thread线程,如果成功则返回0,否则为非0值。发送成功并不意味着thread会终止。

b.

int pthread_setcancelstate(int state,   int *oldstate)  

设置本线程对Cancel信号的反应,state有两种值:PTHREAD_CANCEL_ENABLE(缺省)和PTHREAD_CANCEL_DISABLE,
分别表示收到信号后设为CANCLED状态和忽略CANCEL信号继续运行;old_state如果不为NULL则存入原来的Cancel状态以便恢复。

c.

int pthread_setcanceltype(int type, int *oldtype)  

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

d.

void pthread_testcancel(void)
  •  

pthread_testcancel在不包含取消点,但是又需要取消点的地方创建一个取消点,以便在一个没有包含取消点的执行代码线程中响应取消请求.
线程取消功能处于启用状态且取消状态设置为延迟状态时,pthread_testcancel()函数有效。
如果在取消功能处处于禁用状态下调用pthread_testcancel(),则该函数不起作用。
请务必仅在线程取消线程操作安全的序列中插入pthread_testcancel()。除通过pthread_testcancel()调用以编程方式建立的取消点意外,pthread标准还指定了几个取消点。测试退出点,就是测试cancel信号.

附:什么是取消点:

  • 取消点是线程检查他是否被取消的一个位置

        取消点是如何出现的呢?

  • 对于取消点对于使用某些函数就会出现取消点

  • 例如:sleep,wait,waitpid,waitid,send等函数

e.

int pthread_join(pthread_t thread, void **value_ptr);
  •  

thread:等待退出线程的线程号。
value_ptr:退出线程的返回值。

1.同步取消线程
代码示例:

    #include<stdio.h>  
    #include<stdlib.h>  
    #include <pthread.h>  
    void *thread_fun(void *arg)  
    {  
        int i=1;  
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        /*同步取消,等到下一个取消点再取消*/
        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);   

        printf("thread start \n");  
        while(1)  
        {  
            i++;  
            /*手动建立一个取消点, 防止线程中无取消点,导致线程不取消。*/
            pthread_testcancel();  
        }  
        return (void *)0;  
    }  
    int main()  
    {  
        void *ret=NULL;  
        int iret=0;  
        pthread_t tid;  
        pthread_create(&tid,NULL,thread_fun,NULL);  
        sleep(1);  

        pthread_cancel(tid);//取消线程  
        pthread_join(tid, &ret);  
        printf("thread 3 exit code %d\n", (int)ret);  

        return 0;  

    }  

2.异步取消线程
示例代码:

    #include<stdio.h>  
    #include<stdlib.h>  
    #include <pthread.h>  
    void *thread_fun(void *arg)  
    {  
        int i=1;  
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        /*同步取消,等到下一个取消点再取消*/
//      pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
        /*异步取消, 线程接到取消信号后,立即退出*/
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

        printf("thread start \n");  

        while(1)  
        {  
            i++;  
        }  
        return (void *)0;  
    }  
    int main()  
    {  
        void *ret=NULL;  
        int iret=0;  
        pthread_t tid;  
        pthread_create(&tid,NULL,thread_fun,NULL);  
        sleep(1);  

        pthread_cancel(tid);//取消线程  
        pthread_join(tid, &ret);  
        printf("thread 3 exit code %d\n", (int)ret);  

        return 0;  

    }  

此外,在使用pthread_exit()函数去主动退出线程时(一般放在末尾),如果遇到其它线程发来的pthread_cancel(),那么函数pthread_exit()是不会去执行的,带有返回值时,并且在其它线程,解引用使用该值时,会出现段错误。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值