首页 > 编程语言 > 详细

.取消线程

时间:2014-09-27 00:21:00      阅读:155      评论:0      收藏:0      [点我收藏+]
/*0.取消线程 
  int pthread_cancel(pthread_t thread);
设置取消点
  void pthread_testcancel(void);
测试是否接收到取消请求,如果有,结束线程。
例子:*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
int a = 0;
void *thread1(void *arg)
{
         pthread_testcancel(); 这个函数非常重要要和 pthread_cancel(t1),结合起来使用;
         a = 10; 
} 
/*如果改为:
 int a = 0;
void *thread1(void *arg)
{
         a = 10;
       pthread_testcancel();
 }
运行结果:a值被修改了
main start
main end, a=10*/
int main(int argc, char *argv[])
{       
        pthread_t  t1, t2, t3;
        int ret, i;
        printf("main start\n");
        ret = pthread_create(&t1, NULL, thread1, NULL);
        pthread_cancel(t1);
        pthread_join(t1, NULL);
        sleep(3);
        printf("main end, a=%d\n", a);
      return 0;
}
/*运行结果:在取消点处程序结束,a值未该。
main start
main end, a=0*/

 

.取消线程

原文:http://www.cnblogs.com/leijiangtao/p/3995815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!