zoukankan      html  css  js  c++  java
  • 线程分离pthread_detach()中的return()和pthread_exit()

     1 #include <stdio.h>
     2 #include <pthread.h>
     3 
     4 pthread_t tid[2];
     5 void *func_2(void *arg);
     6 void *func_1(void *arg)
     7 {       //为函数func_2创建线程
     8         printf("Starting func_1
    ");
     9         pthread_create(&tid[1], NULL, func_2, NULL);
    10         //tid[0]将自己挂起,等待线程tid[1]的结束
    11         pthread_join(tid[1], NULL);
    12 }
    13 
    14 void *func_2(void *arg)
    15 {
    16         printf("Starting func_2
    ");
    17         sleep(1);
    18         pthread_exit(NULL);
    19 }
    20 
    21 int main()
    22 {       //为函数func_1创建线程并将其分离
    23         pthread_create(&tid[0], NULL, func_1, NULL);
    24 //      pthread_detach(tid[0], NULL);   //分离
    25         pthread_detach(tid[0]); //分离
    26         //主线程不退出,但这两个现成的资源都可以释放掉
    27 //      return 0;
    28         pthread_exit(NULL);
    29 }

    在main()函数中,如果采用return 0,则没有任何输出就退出了;而如果采用的是pthread_exit(NULL),则打印出消息

  • 相关阅读:
    Linux 系统中用户切换(su user与 su
    linux 用户打开进程数和文件数调整
    hive sql 语法详解
    iOS
    iOS
    MySQL的事务的处理
    iOS
    iOS AOP编程思想及实践
    iOS 静态库和动态库(库详解)
    iOS 沙盒目录结构及正确使用
  • 原文地址:https://www.cnblogs.com/fallenmoon/p/6769295.html
Copyright © 2011-2022 走看看