如何不使用pthread_cancel而杀死线程
线程 如何 杀死 pthread 使用 cancel
2023-09-27 14:24:26 时间
http://www.cnblogs.com/no7dw/archive/2012/09/27/2705847.html
During the time I use standalone cross compliers to build my system, I find there is NO pthread_cancel in pthread.h (/home/dengwei/standalone-toolchain/sysroot/usr/include/pthread.h).
Shocked by that, but here comes the solution, by using pthread_kill to send a signal , and adding a signal handler :
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <sys/types.h> #include <errno.h> pthread_t pid; void handle_quit(int signo) { printf("in qq handle sig %d \n", signo); pthread_exit(NULL); } void* test(void *arg) { signal(SIGQUIT,handle_quit ); for(int i=0;i<100;i++) { printf("in pthread test \n"); sleep(1); } } int main(void) { printf("begin \n"); pthread_create(&pid, NULL , test, NULL); sleep(3); if(pthread_kill(pid, 0)!= ESRCH) { printf("thread %d exists!\n", pid); pthread_kill(pid, SIGQUIT); // pthread_exit(NULL);//this won't work printf("after kill\n"); } sleep(1); printf("exit in main\n"); }
相关文章
- C#如何优雅的结束一个线程
- 如何停止一个线程池?
- Executors创建线程池有哪几种方式?
- 如何优雅地停止一个线程?
- 如何创建、启动 Java 线程?
- Java线程的6种状态及切换(透彻讲解)
- 详解线程同步和线程互斥,Java如何实现线程同步和互斥
- 干货 | 教你如何监控 Java 线程池运行状态
- c#实例-子线程查找另一线程模态对话框句柄(invoke测试)
- 线程系列02,多个线程同时处理一个耗时较长的任务以节省时间
- Map容器线程安全问题
- 任务、进程和线程
- JNI入门课程-第七章:JNI线程通知安卓刷新
- 浅析Java并发编程:如何做到线程安全-Collections.synchronizedList和CopyOnWriteArrayList的2种实现
- 浅析如何使用Spring的@Async异步任务、自定义线程池及异常处理
- 多线程之线程简介
- 顺序打印(有三个线程,线程名称分别为:a,b,c...)
- Android学习之线程如何多次实现start
- python进程和线程
- 如何查看线程运行在哪个CPU上面
- 你知道如何安全正确的关闭线程池吗?