pthread_join pthread_exit pthread_detach

pthread_join     pthread_exit  
  
函數pthread_join用來等待一個線程的結束。函數原型爲:
  extern int pthread_join __P ((pthread_t __th, void
**__thread_return));
 
 第一個參數爲被等待的線程標識符,第二個參數爲一個用戶定義的指針,它可以用來存儲被等待線程的返回值。這個函數是一個線程阻塞的函數,調用它的函數將
一直等待到被等待的線程結束爲止,當函數返回時,被等待線程的資源被收回。一個線程的結束有兩種途徑,一種是象我們上面的例子一樣,函數結束了,調用它的
線程也就結束了;另一種方式是通過函數pthread_exit來實現。它的函數原型爲:
  extern void pthread_exit __P ((void *__retval)) __attribute__
((__noreturn__));
 
 唯一的參數是函數的返回代碼,只要pthread_join中的第二個參數thread_return不是NULL,這個值將被傳遞給
thread_return。最後要說明的是,一個線程不能被多個線程等待,否則第一個接收到信號的線程成功返回,其餘調用pthread_join的線程則返回錯誤代碼ESRCH。


int pthread_detach( pthread_t pid );

//參數tid 是希望等待的線程的線程號, 把指定的線程轉變爲脫離狀態
一個線程或者是可匯合的(joinable,缺省值),或者是脫離的(detached)。當一個可匯合的線程終止時,它的線程ID和退出狀態將留到另一個線程對它調用pthread_join。脫離線程卻象守護進程:當它們終止的時,所有相關資源都被釋放,我們不能等待它們終止。如果一個線程需要知道另一個線程什麼時候終止,那就最好使第二個線程的可匯合狀態。

下面 通過例子說明:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define THREAD_NUMBER 2
int retval_hello1= 1, retval_hello2 = 2;

void* hello1(void *arg)
{   
   char *hello_str = (char *)arg;
   sleep(2);
   printf("%s/n", hello_str);
   pthread_exit(&retval_hello1);
}

void* hello2(void *arg)
{
   char *hello_str = (char *)arg;
   sleep(1);
   printf("%s/n", hello_str);
   pthread_exit(&retval_hello2);
}

int main(int argc, char *argv[])
{
   int i;
   int ret_val;
   int *retval_hello[2];
  
   pthread_t pt[THREAD_NUMBER];
   const char *arg[THREAD_NUMBER];
   arg[0] = "hello world from thread1";
   arg[1] = "hello world from thread2";
  
   printf("Begin to create threads.../n");
   ret_val = pthread_create(&pt[0], NULL, hello1, (void *)arg[0]);
   if (ret_val != 0 ) {
      printf("pthread_create error!/n");
      exit(1);
   }

   ret_val = pthread_create(&pt[1], NULL, hello2, (void *)arg[1]);
   if (ret_val != 0 ) {
      printf("pthread_create error!/n");
      exit(1);
   }
  
   printf("Begin to wait for threads.../n");  
   for(i = 0; i < THREAD_NUMBER; i++) {
       ret_val = pthread_join(pt[i], (void **)&retval_hello[i]);
       if (ret_val != 0) {
          printf("pthread_join error!/n");
      exit(1);
       } else {
          printf("return value is %d/n", *retval_hello[i]);
       }
   }
   printf("Now, the main thread returns./n");
   return 0;
}

執行結果爲:

Begin to create threads...
Begin to wait for threads...
hello world from thread2
hello world from thread1
return value is 1
return value is 2
Now, the main thread returns.

線程1,2的執行順序可以通過sleep來調節,但是主線程必須在子線程完成之後才能執行,即打印”Now, the main thread returns.“。此外,因爲調用pthread_join()的順序,必定是線程1先執行“return value is xx”,不管線程2是否先執行完。

下面修改pthread_join爲pthread_detach(),代碼爲

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define THREAD_NUMBER 2
int retval_hello1= 1, retval_hello2 = 2;

void* hello1(void *arg)
{   
   char *hello_str = (char *)arg;
   sleep(2);
   printf("%s/n", hello_str);
   pthread_exit(&retval_hello1);
}

void* hello2(void *arg)
{
   char *hello_str = (char *)arg;
   sleep(1);
   printf("%s/n", hello_str);
   pthread_exit(&retval_hello2);
}

int main(int argc, char *argv[])
{
   int i;
   int ret_val;
   int *retval_hello[2];
  
   pthread_t pt[THREAD_NUMBER];
   const char *arg[THREAD_NUMBER];
   arg[0] = "hello world from thread1";
   arg[1] = "hello world from thread2";
  
   printf("Begin to create threads.../n");
   ret_val = pthread_create(&pt[0], NULL, hello1, (void *)arg[0]);
   if (ret_val != 0 ) {
      printf("pthread_create error!/n");
      exit(1);
   }

   ret_val = pthread_create(&pt[1], NULL, hello2, (void *)arg[1]);
   if (ret_val != 0 ) {
      printf("pthread_create error!/n");
      exit(1);
   }
  
   printf("Begin to wait for threads.../n");  
   for(i = 0; i < THREAD_NUMBER; i++) {
       ret_val = pthread_detach(pt[i]);
       if (ret_val != 0) {
          printf("pthread_join error!/n");
      exit(1);
       }
   }
   printf("Now, the main thread returns./n");
   return 0;
}

執行結果爲

Begin to create threads...
Begin to wait for threads...
Now, the main thread returns.
線程1,2沒有執行(也可能執行),因爲子線程爲可分離的,主線程在執行完之後即將進程銷燬,資源收回,導致子線程未運行。可以在return 0 語句之前加入sleep(5),這樣執行結果爲

Begin to create threads...
Begin to wait for threads...
Now, the main thread returns.
hello world from thread2
hello world from thread1
所以,pthread_join()會掛起父線程,直至子線程完成纔可以執行後面的代碼,此外,一個PTHREAD_CREATE_JOINABLE狀態的子線程不會自動釋放該線程的內存資源,包括線程描述符和其使用的棧;而主線程調用pthread_detach()時,無需等待子線程的完成,它可以立即執行後面的代碼,當然,也有可能主線程執行完之後銷燬進程,導致子線程未能執行,此外,一個PTHREAD_CREATE_DETACH狀態的子線程擁有自我回收內存資源的功能。

發佈了13 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章