c - 不使用pthread_detach或pthread_join,不会为其他新创建的线程清理资源吗?

标签 c linux multithreading pthreads

Below is my code snippet.



int main ( )
    {
     some instructions;
     while ( 1 )
     {
       /* Block 1 : Starts*/
       if ( selection == 1 )
       {
         ret = pthread_create ( &tid, NULL, &select_n_process_req, NULL );
         if ( ret != 0 )
         {
           printf ("Error Creating Thread");
         }
       }
       /* Block 1 : Ends*/

       /* Block 2 : Starts*/
       printf ("Sleeping for [%d]", retry_time * 60 * 60);
       for ( i = 0; i < retry_time * 60 * 60; i++ )
       {
          if ( stop_flag == 1 )
          {
           printf ("Process Stopped\n");
           break;
          }
          sleep ( 1 );
       }
       /* Block 2 : ENDS*/
     }
     some instructions;
     return SUCCESS;
    }

    int select_n_process_req ( void )
    {
     some instructions;
     return SUCCESS;
    }

代码说明:

  1. block 1:变量“selection”始终为 1,并且线程 总是为了执行某些任务而创建。
  2. block 2:线程创建后,该 block 将等待 再次“retry_time(通常会1小时)”创建一个新线程 完成同样的任务。

问题:

  1. 我没有调用任何 pthread_join 或 pthread_detach,但我 从函数返回(这将终止线程) “选择_n_进程_请求”。这样会不会清空分配的资源 pthread_create?
  2. 我发现奇怪的是,经过一些加载后,pthread_create 没有 根本没有打电话。这是因为我没有使用 pthread_detach 或 pthread_join?

谢谢。

最佳答案

您必须调用pthread_join(从父线程)或pthread_detach,或者通过将附加属性传递给pthread_create来创建处于分离状态的线程强>。参见这里:http://man7.org/linux/man-pages/man3/pthread_create.3.html

关于c - 不使用pthread_detach或pthread_join,不会为其他新创建的线程清理资源吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24816518/

相关文章:

r - 多线程 Flask 应用程序导致 rpy2 R 进程中出现堆栈错误

java - 线程自动名称更改

c - GCC默认参数(-Werror -Wall..)

linux - yum 缓存中的镜​​像

Python kivy存储模块

linux - 为什么 "cat a.txt | xargs vi"会破坏 bash?

c - 不干净地从 C 子进程退出而没有 valgrind 提示?

c++ - 无法在 C、C++ 和 Obj-C 中编译并带有特定错误消息的代码

C:Memcpy vs Shifting:哪个更有效率?

c# - 等待动态创建的任务直到它们完成