简体   繁体   English

不使用pthread_detach或pthread_join,是否不会为其他新创建的线程清除资源?

[英]Not using pthread_detach or pthread_join, will not clean up the resources for other newly created threads?

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;
    }

Explanation of the Code: 代码说明:

  1. Block 1: The variable "selection" will always be 1 and the thread will be created always for doing some task. 块1:变量“选择”将始终为1,并且将始终为执行某些任务而创建线程。
  2. Block 2: Once the thread is created, this blocks waits for "retry_time ( usually will 1 hour )" again to create a new thread for doing the same task. 块2:创建线程后,该块再次等待“ retry_time(通常为1小时)”,以创建一个新线程来执行相同的任务。

Question: 题:

  1. I am not calling any pthread_join or pthread_detach, but I am returning(which will terminate the thread) from the function "select_n_process_req". 我没有调用任何pthread_join或pthread_detach,而是从函数“ select_n_process_req”返回(将终止线程)。 Will this not clear the resources allocated to pthread_create? 这会不会清除分配给pthread_create的资源?
  2. I found it strange that after some load, pthread_create was not called at all. 我发现奇怪的是,在进行某些加载之后,根本没有调用pthread_create。 Is this because I am not using pthread_detach or pthread_join? 这是因为我没有使用pthread_detach或pthread_join吗?

Thank You. 谢谢。

You have to either call pthread_join (from parent thread), or pthread_detach , or create the thread in detached state by passing additional attributes to pthread_create . 您必须调用pthread_join (从父线程)或pthread_detach ,或者通过将其他属性传递给pthread_create来以分离状态创建线程。 See here: http://man7.org/linux/man-pages/man3/pthread_create.3.html 看到这里: http : //man7.org/linux/man-pages/man3/pthread_create.3.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM