zoukankan      html  css  js  c++  java
  • pthread_exit

    pthread_exit:
    • By having main() explicitly call pthread_exit() as the last thing it does, main() will block and be kept alive to support the threads it created until they are done.

    就是说,main函数中调用pthread_exit的时候,进程不会退出,所以main创建的线程就不会退出,但是main的局部变量存储的堆栈应该已经释放了。见如下代码:

    /*****************************************************************************
    * FILE: hello_arg3.c
    * DESCRIPTION:
    *   This "hello world" Pthreads program demonstrates an unsafe (incorrect)
    *   way to pass thread arguments at thread creation.  In this case, the
    *   argument variable is changed by the main thread as it creates new threads.
    * AUTHOR: Blaise Barney
    * LAST REVISED: 07/16/14
    ******************************************************************************/
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define NUM_THREADS     8
    
    void *PrintHello(void *threadid)
    {
       long taskid;
       sleep(1);
       taskid = *(long *)threadid;
       printf("Hello from thread %ld
    ", taskid);
       pthread_exit(NULL);
    }
    
    int main(int argc, char *argv[])
    {
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    
    for(t=0;t<NUM_THREADS;t++) {
      printf("Creating thread %ld
    ", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);
      if (rc) {
        printf("ERROR; return code from pthread_create() is %d
    ", rc);
        exit(-1);
        }
       }
    
    pthread_exit(NULL);
    }

    输出:

    Creating thread 0
    Creating thread 1
    Creating thread 2
    Creating thread 3
    Creating thread 4
    Creating thread 5
    Creating thread 6
    Creating thread 7
    Hello from thread 140737488348392
    Hello from thread 140737488348392
    Hello from thread 140737488348392
    Hello from thread 140737488348392
    Hello from thread 140737488348392
    Hello from thread 140737488348392
    Hello from thread 140737488348392
    Hello from thread 140737488348392

    当我们在pthread_exit(NULL)的前面加sleep(10)的时候输出是:

    Creating thread 0
    Creating thread 1
    Creating thread 2
    Creating thread 3
    Creating thread 4
    Creating thread 5
    Creating thread 6
    Creating thread 7
    Hello from thread 8
    Hello from thread 8
    Hello from thread 8
    Hello from thread 8
    Hello from thread 8
    Hello from thread 8
    Hello from thread 8
    Hello from thread 8

     另外需注意:void *类型表示没有指定类型的指针

    参考资料:https://computing.llnl.gov/tutorials/pthreads/

  • 相关阅读:
    Flutter 中那么多组件,难道要都学一遍?
    【Flutter实战】自定义滚动条
    Python 为什么只需一条语句“a,b=b,a”,就能直接交换两个变量?
    一篇文章掌握 Python 内置 zip() 的全部内容
    Python 3.10 的首个 PEP 诞生,内置类型 zip() 迎来新特性
    Python 3.10 版本采纳了首个 PEP,中文翻译即将推出
    Python 为什么不支持 i++ 自增语法,不提供 ++ 操作符?
    Python 为什么推荐蛇形命名法?
    Python 为什么没有 main 函数?为什么我不推荐写 main 函数?
    Python 为什么不用分号作终止符?
  • 原文地址:https://www.cnblogs.com/albert1017/p/3928898.html
Copyright © 2011-2022 走看看