Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4480148
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-05-14 22:30:50

取消线程:
    功能:取消一个正在执行线程的操作。一个线程能够被取消需要满足条件
    条件:一:该线程是否可以被其他取消是可设置的,
             PTHREAD_CANCEL_DISABLE   PTHREAD_CANCEL_ENABLE
          二:该线程处于可取消点才能取消


typedef unsigned long pthread_t  // %lu

需要在 可取消状态 PTHREAD_CANCEL_ENABLE 是,才能被取消
extern int pthread_cancel (pthread_t __cancelthread)


设置可取消状态
查询当前线程的可取消性状态
extern int pthread_setcancelstate(int __state, int *__oldstate);

设置取消类型
extern int pthread_setcanceltype(int __type,int *__oldstate)
                                     type = PTHREAD_CANCEL_ENABLE
                                     type = PTHREAD_CANCEL_DISABLE




函数描述:
    主线程使用 pthread_cancle 取消线程。但是在子线程中用 pthread_setcancelstate 设置了 PTHREAD_CANCEL_DISABLE, 所以不能被取消,主线程处于等待状态。经过一段时间,子线程 设置为
PTHREAD_CANCEL_ENABLE ,现在就可以取消子线程操作了,主线程就取消了子线程的运行


代码: pthread_cancel.rar  
        gcc main.c -lpthread -o main

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>

  4. void *thread_fun(void *a);

  5. int main(int argc, char *argv[])
  6. {
  7.     int res;
  8.     pthread_t a_thread;//typedef unsigned long pthread_t &lu
  9.     void *thread_result;

  10.     res = pthread_create(&a_thread, NULL, (void *)*thread_fun, NULL); //建立子线程
  11.     if(res != 0)
  12.     {
  13.         perror("thread create failed");
  14.         exit(1);
  15.     }

  16.     sleep(10);
  17.     
  18.     printf("cancle thread...\n");
  19.     res = pthread_cancel(a_thread); //取消子线程
  20.     if(res != 0)
  21.     {
  22.         perror("thread cancle failed");
  23.         exit(1);
  24.     }

  25.     printf("waiting for thread to finish...\n");
  26.     res = pthread_join(a_thread, &thread_result); //退出 子线程
  27.     if(res != 0)
  28.     {
  29.         perror("thread join failed");
  30.         exit(1);
  31.     }
  32.     
  33.     exit(EXIT_SUCCESS);
  34. }

  35. void *thread_fun(void *a)
  36. {
  37.     int i,res,j; //存放在 栈中 临时变量
  38.     sleep(1);
  39.     
  40.     res = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); //设置取消状态 DISABLE
  41.     if(res != 0)
  42.     {
  43.         perror("pthread setcanclestate failed");
  44.         exit(EXIT_FAILURE);
  45.     }
  46.     
  47.     sleep(3);
  48.     
  49.     printf("thread cancel type is disable ,can't cancle this thread\n"); //打印不可取状态信息
  50.     for(i = 0; i < 3; i++)
  51.     {
  52.         printf("thread is running(%d)...\n",i);
  53.         sleep(1);
  54.     }

  55.     res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  56.     if(res != 0)
  57.     {
  58.         perror("pthread_setcanclestat failed");
  59.         exit(EXIT_FAILURE);
  60.     }
  61.     else
  62.     {
  63.         printf("now change the canclestate is ENABLE\n");
  64.     }
  65.     sleep(200);
  66.     
  67.     pthread_exit(NULL); //没有返回到 主线程信息
  68.         
  69. }

  1. ywx@yuweixian:~/yu/professional/4$ ./pthread_cancel
  2. thread cancel type is disable ,can't cancle this thread
  3. thread is running(0)...
  4. thread is running(1)...
  5. thread is running(2)...
  6. now change the canclestate is ENABLE
  7. cancle thread...
  8. waiting for thread to finish...



阅读(1674) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~