线程取消
int pthread_cancel(pthread_t th);
該函數運行一個線程取消指定的另一個線程th
函數成功,返回0,否則,返回非0;
/*** cancel.c ***/ #include<stdio.h> #include<pthread.h> #include<errno.h> #include<string.h> #include<stdlib.h>void * func(void *arg) {while(1){printf("fun run...\n");sleep(1);}return NULL; }int main() {pthread_t t1;int err = pthread_create(&t1,NULL,func,NULL);if( 0 != err){printf("thread_create failled : %s\n",strerror(errno));}else{printf("thread_create success\n");}sleep(5);pthread_cancel(t1);pthread_join(t1,NULL);return EXIT_SUCCESS; }函數運行結果:
exbot@ubuntu:~/wangqinghe/thread/20190729$ ./cancel
thread_create success
fun run...
fun run...
fun run...
fun run...
fun run...
轉載于:https://www.cnblogs.com/wanghao-boke/p/11264648.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: 线程终止
- 下一篇: java中将一个类的静态属性的访问权限设