Linux学习笔记23——取消线程
2014-01-04 10:05
375 查看
一 相关函数
1 发送终止信号
2 设置取消状态
3 设置取消类型
如果取消进程请求被接受了,线程可以进入第二个控制层次,用pthread_setcanceltype
默认情况下,线程在启动时的取消状态为PTHREAD_CANCEL_ENABLE,取消类型是PTHREAD_CANCEL_DEFERRED
二 线程取消的语义
线程取消的方法是向目标线程发Cancel信号(pthread_cancel函数发送Cancel信号),但如何处理Cancel信号则由目标线程自己决定,或者忽略、或者立即终止、或者继续运行至Cancelation-point(取消点),由不同的Cancelation状态(pthread_setcancelstate函数设置状态)决定。
线程接收到CANCEL信号的缺省处理(即pthread_create()创建线程的缺省状态)是继续运行至取消点,也就是说设置一个CANCELED状态,线程继续运行,只有运行至Cancelation-point的时候才会退出。
三 例子
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); }
![](http://images.cnitblog.com/blog/525372/201401/04100024-e65d5aaea69d4e88893acaacf187da0c.png)
相关文章推荐
- Linux进程线程学习笔记:进程创建
- Linux进程线程学习笔记:运行新程序
- Linux进程与线程学习笔记(更新)
- 你不知道的Java学习笔记23-- 线程优先级
- linux下C编程学习笔记之线程学习(二)
- 【Linux学习笔记】30:declare命令(声明/取消/查看变量类型)
- linux下C编程学习笔记之线程学习(一)
- Linux学习笔记之内核线程
- Linux程序设计学习笔记----异步信号与线程属性控制
- linux命令学习笔记(23):Linux 目录结构
- UNIX环境编程学习笔记(28)——多线程编程(三):线程的取消
- Linux学习笔记23 --LDAP网络用户账户
- Linux进程线程学习笔记
- linux 系统编程-学习笔记9--线程的同步互斥锁、读写锁/select/poll
- Linux进程线程学习笔记:进程控制
- Linux进程线程学习笔记:进程创建
- Linux网管学习笔记(23)几点学习体会(待续)
- Linux进程线程学习笔记 系列转
- Linux进程间通信IPC学习笔记之同步一(线程、互斥锁和条件变量)
- Linux进程线程学习笔记:运行新程序