zoukankan      html  css  js  c++  java
  • 取消线程,是否会释放线程的所有资源?

    #include <stdlib.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <sched.h>
    //取消线程,是否会释放线程的所有资源?例子:
    void *thread1(void *arg)
    {
            printf("start thread (%u)
    ", (unsigned)pthread_self());
    }       
    int main(int argc, char *argv[])
     {       
            pthread_t  t1, t2, t3;
             int ret;
           printf("main start
    ");
            do{     
                    ret = pthread_create(&t1, NULL, thread1, NULL);
                    if(ret != 0)
                     {       
                            printf("create thread failed
    ");
                            exit(1);
                    }
                    pthread_cancel(t1);
                    printf("<<<<<<");//much too importent这行代码很重要
                 
                    //pthread_join(t1, NULL);这句加上,将不断创建新线程.
                    if(ret != 0)
                    {       
                            printf("join failed
    ");
                            exit(1);
                    }
            }while(1);
         return 0;
     }   
    //运行结果:
    /*start thread (349191056), 327
    start th<<<<<<<<<<<<<<<<<<<<<<<create thread failed
    注意:每次运行的结果都不一样,在主线程里面加了printf限制产生线程的速度,但是能生成的线程数都在350个左右,应该可以判断,取消并没完全释放资源。所以取消线程后,还应该用join来完全释放资源.*/
    //注意:取消线程相当于使用pthread_exit终止线程。
  • 相关阅读:
    商场活动|简单易用|可下载试用|复用转盘抽奖软件
    js dictionary
    财务大写
    SET ANSI_NULLS ON ……
    批量生成clr脚本
    Git
    CTE递归查询
    jquery 巧用json传参
    个人犯的一个golang routine错误
    .NET实现自动编译
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/3995819.html
Copyright © 2011-2022 走看看