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

代碼說明:

  1. 塊1:變量“選擇”將始終為1,並且將始終為執行某些任務而創建線程。
  2. 塊2:創建線程后,該塊再次等待“ retry_time(通常為1小時)”,以創建一個新線程來執行相同的任務。

題:

  1. 我沒有調用任何pthread_join或pthread_detach,而是從函數“ select_n_process_req”返回(將終止線程)。 這會不會清除分配給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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM