取消线程,是否会释放线程的所有资源?

#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终止线程。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值