Linux学习笔记23——取消线程

一 相关函数

  1 发送终止信号

#include <pthread.h>
int pthread_cancel(pthread_t thread);

  2 设置取消状态

#include <pthread.h>
int pthread_setcancelstate(int state,    //取值:1 PTHREAD_CANCEL_ENABLE,这个值允许线程接受取消请求
                          //    2 PTHREAD_CANCEL_DISABLE,它的作用是忽略取消请求
                int *oldstate   //用于获取先前的取消状态
               );

  3 设置取消类型
  如果取消进程请求被接受了,线程可以进入第二个控制层次,用pthread_setcanceltype

#include <pthread.h>
int pthread_setcanceltype(int type,     //取值:1 PTHREAD_CANCEL_ASYNCHRONOUS,它将使得在接收到取消请求后立即采取行动
                         //   2 PTHREAD_CANCEL_DEFERRED,它将使得在接收到取消请求后,一直等待直到线程执行了下述函数之一后才采取行动
                               pthread_join,pthread_cond_wait,pthread_cond_timewait,pthread_testcancel,sem_wait或sigwait
               int *oldtype   //可以保存先前的状态,如果不想知道先前的状态,可以传递NULL给它
               );

  默认情况下,线程在启动时的取消状态为PTHREAD_CANCEL_ENABLE,取消类型是PTHREAD_CANCEL_DEFERRED

 

二 线程取消的语义

  线程取消的方法是向目标线程发Cancel信号(pthread_cancel函数发送Cancel信号),但如何处理Cancel信号则由目标线程自己决定,或者忽略、或者立即终止、或者继续运行至Cancelation-point(取消点)由不同的Cancelation状态(pthread_setcancelstate函数设置状态)决定。

  线程接收到CANCEL信号的缺省处理(即pthread_create()创建线程的缺省状态)是继续运行至取消点,也就是说设置一个CANCELED状态,线程继续运行,只有运行至Cancelation-point的时候才会退出。

 

三 例子

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_function(void *arg);

int main(){
    pthread_t a_thread;
    int res;
    void *thread_result;
    
    res=pthread_create(&a_thread,NULL,thread_function,NULL);
    if(res!=0){
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
    sleep(3);      //主线程等待3秒
    printf("Canceling thread...\n");
    res=pthread_cancel(a_thread);  //发送取消线程请求
    if(res!=0){
        perror("Thread canceling failed\n");
        exit(EXIT_FAILURE);
    }
    printf("Waiting for thread to finish...\n");
    res=pthread_join(a_thread,&thread_result);    //终止线程
    if(res!=0){
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg){
    int i,res;
    res=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);    //设置取消状态为允许接受取消请求
    if(res!=0){
        perror("Thread pthread_setcancelstate failed");
        exit(EXIT_FAILURE);
    }
    res=pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);   //设置取消类型为等待执行函数后停止
    if(res!=0){
        perror("Thread pthread_setcanceltype failed");
        exit(EXIT_FAILURE);
    }
    printf("thread_function is running\n");
    for(i=0;i<10;i++){
        printf("Thread is still running (%d)...\n",i);
        sleep(1);
    }
    pthread_exit(0);
}

 

 

posted @ 2014-01-04 10:05  LOSER Z  阅读(477)  评论(0编辑  收藏  举报