#inlude <pthread.h> int pthread_cancel(pthread_t thread);这个函数提供一个线程标识符就可以发送请求来取消它。
#include <pthread.h> int pthread_setcancelstate(int state, int *oldstate);第一个参数的取值可以是PTHREAD_CANCEL_ENABLE,这个值允许线程接收取消请求;或者是PTHREAD_CANCEL_DISABLE,它的作用是忽略取消请求。oldstate指针用于获取先前的取消状态。
#include <pthread.h> int pthread_setcanceltype(int type, int *oldtype);type参数可以有两种取值:一个是PTHREAD_CANCEL_ASYNCHRONOUR,它将使得在接收到取消请求后立即采取行动;另一个是PTHREAD_CANCEL_DEFERRED,它将使得在接受到取消请求后,一直等待直到线程执行下述函数之一才采取行动。具体是函数pthread_join,pthread_cond_wait,pthread_cond_timedwait,pthread_testcancel,sem_wait或sigwait.
/*************************************************************************
 > File Name:    thread7.c
 > Description:  thread7.c程序在主线程中向它创建的新线程发送一个取消请求
 > Author:       Liubingbing
 > Created Time: 2015/7/7 9:58:40
 > Other:        thread7.c程序中新线程的调用函数中分别需要设置新线程的取消状态和取消类型
 ************************************************************************/
#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thread_function(void *arg);
int main(){
	int res;
	pthread_t a_thread;
	void *thread_result;
	/* pthread_create函数创建新线程,新线程标识符保存在a_thread,新线程调用的函数为thread_function,函数的参数为NULL */
	res = pthread_create(&a_thread, NULL, thread_function, NULL);
	if (res != 0) {
		perror("Thread creation failed");
		exit(EXIT_FAILURE);
	}
	sleep(3);
	printf("Canceling thread...\n");
	/* pthread_cancel函数请求线程a_thread终止 */
	res = pthread_cancel(a_thread);
	if (res != 0) {
		perror("Thread cancelation failed");
		exit(EXIT_FAILURE);
	}
	printf("Waiting for thread to finish...\n");
	/* pthread_join等待线程a_thread与主线程重新合并 */
	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;
	/* pthread_setcancelstate函数设置线程的取消状态,PTHREAD_CANCEL_ENABLE允许线程接收取消请求;PTHREAD_CANCEL_DISABLE忽略取消请求 */
	res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
	if (res != 0) {
		perror("Thread pthread_setcancelstate failed");
		exit(EXIT_FAILURE);
	}
	/* pthread_setcanceltype函数设置线程的取消类型,PTHREAD_CANCEL_DEFERRED将使得在接收到取消请求后,一直等待直到线程执行某个函数(如pthread_join)之后才采取行动 */
	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);
}以通常的方法创建新线程后,主线程休眠一会儿(好让新线程有时间开始执行),然后发送一个取消请求,如下所示:sleep(3);
printf("Canceling thread...\n");
res = pthread_cancel(a_thread);在新创建的线程中,首先将取消状态设置为允许取消,如下所示:res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);然后将取消类型设置为延迟取消,如下所示:
res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);最后,线程在循环中等待被取消,如下所示:
for (i = 0; i < 10; i++) {
    printf("Thread is still running (%d)...\n", i);
    sleep(1);
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/yiranant/article/details/46785699