线程取消
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...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2018-07-29 1002 A+B for Polynomials (25)
2018-07-29 1001 A+B Format (20)