取消线程,是否会释放线程的所有资源?
2014-09-26 23:39
295 查看
#include <stdlib.h> #include <pthread.h> #include <stdio.h> #include <sched.h> //取消线程,是否会释放线程的所有资源?例子: void *thread1(void *arg) { printf("start thread (%u)\n", (unsigned)pthread_self()); } int main(int argc, char *argv[]) { pthread_t t1, t2, t3; int ret; printf("main start\n"); do{ ret = pthread_create(&t1, NULL, thread1, NULL); if(ret != 0) { printf("create thread failed\n"); exit(1); } pthread_cancel(t1); printf("<<<<<<");//much too importent这行代码很重要 //pthread_join(t1, NULL);这句加上,将不断创建新线程. if(ret != 0) { printf("join failed\n"); exit(1); } }while(1); return 0; } //运行结果: /*start thread (349191056), 327 start th<<<<<<<<<<<<<<<<<<<<<<<create thread failed 注意:每次运行的结果都不一样,在主线程里面加了printf限制产生线程的速度,但是能生成的线程数都在350个左右,应该可以判断,取消并没完全释放资源。所以取消线程后,还应该用join来完全释放资源.*/ //注意:取消线程相当于使用pthread_exit终止线程。
相关文章推荐
- 同一线程多次创建 的 线程资源释放问题在每个线程的一开始,用pthred_detach(pthread_self())来使自己detach掉
- 请问如何强行释放所有已关闭的程序所残留的内存等资源?类似于微软的那个叫mem.exe的工具,如何入手呢?
- 判断线程是否释放
- 线程资源释放
- DotNet下使用GDI+绘图时资源是否需要释放
- 如何线程终止时释放资源?-
- 彻底释放Linux线程的资源
- C# C#如何判断线程池中所有的线程是否已经完成,Backgroudworker
- 线程资源释放CPU
- 线程结束资源释放 线程终止
- C#如何判断线程池中所有的线程是否已经完成
- 【讨论】:多线程访问某资源,所有操作为“只读操作”,无写入操作。是否需要加锁?
- linux多线程之线程资源的释放
- 线程资源释放
- pthread_cancel 线程取消以及锁的释放
- 线程的退出与其相关资源的释放
- 应用程序结束时的资源释放及线程问题
- 使用pthread 线程退出时自动释放资源
- 线程的资源释放(一)
- linux下du和df命令的区别 ---检查文件资源是否被释放