2015-09-15 55 views
0

我正在測試基於Linux 2.6.21和pThread庫。 我嘗試了幾個案例,以找出解決方法如何避免主進程終止。但是,我沒有發現它。 請告訴我爲什麼退出線程函數導致主進程終止? 這裏是下面的測試代碼,爲什麼pThread退出導致主進程終止?

#include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <unistd.h> 
    #include <signal.h> 
    #include <sys/time.h> 
    #include <sys/msg.h> 
    #include <sys/types.h> 
    #include <sys/ioctl.h> 
    #include <sys/signal.h> 
    #include <linux/input.h> 
    #include <fcntl.h> 
    #include <errno.h> 
    #include <dlfcn.h> 
    #include <time.h> 
    #include <pthread.h> 

    int handle = 0; 
    int main_loop = 0; 

    void *testThread(void *pParm) 
    { 
     int i; 
     for (i=0; i < 5 ; i++){ 
     printf("====testThread loop %d\n", i); 
     sleep(1); 
     } 

     if (main_loop == 1){ 
     exit(0); 
     } 
     else if (main_loop == 2) 
     { 
     sleep(10); 
     exit(0); 
     } 
     else if (main_loop == 3) 
     { 
     pthread_exit(NULL); 
     } 
     else if (main_loop == 4) 
     { 
     sleep(10); 
     pthread_exit(NULL); 
     } 
    } 

    int main(int argc, char *argv[]) 
    { 
     pthread_t pTestThread; 

     int i, ret; 

     if (argc == 2){ 
      main_loop = atoi(argv[1]); 
     } 
     if (argc == 3){ 
      main_loop = atoi(argv[1]); 
      handle = atoi(argv[2]); 
     } 

     ret = pthread_create(&pTestThread, NULL, (void *)testThread, NULL); 
     if (0 == ret){ 
     if (handle == 0) 
      pthread_detach(pTestThread); 
     printf("====Thread creation okay!\n"); 
     }else{ 
     printf("====Thread creation error!\n"); 
     return 0; 
     } 

     if (handle == 1) 
     { 
     printf("====pthread_join waiting\n"); 
     pthread_join(pTestThread, (void **)&ret); 
     printf("====pthread_join ret %d\n", ret); 
     } 

     for (i=0; i < 20; i++) 
     { 
     printf("====Main loop %d\n", i); 
     sleep(1); 
     } 

     printf("====Main Exit\n"); 
     return 0; 
    } 

在該代碼中,我從未見過「====主退出」的日誌用的各種組合(自變量第二和第三)。

+0

請發佈一個完整的程序(包括全局變量,頭文件),以及在意外終止時使用的命令行輸入(或者 - 更好 - 刪除與意外終止無關的代碼,因此不存在依賴關係在命令行參數上)。 –

+0

main_loop的價值是什麼? – immibis

+0

什麼是「pThread退出」? –

回答

1

。請告訴我爲什麼退出線程功能導致主進程 終止?

是的,在你的線程函數中,你使用「exit()」,這個函數可以是「Terminates the process normally, performing the regular cleanup for terminating programs」。

你可以看到http://www.cplusplus.com/reference/cstdlib/exit/瞭解更多詳情:

調用此函數銷燬靜態持續時間的所有對象:一個 程序多線程運行,不得調用exit(見 quick_exit一個類似的功能不影響靜態 對象)。

所以如果你使用pthread_exit,而不是退出,你可以看到

====Main loop 18 
====Main loop 19 
====Main Exit 

順便說一句,

ret = pthread_create(&pTestThread, NULL, (void *)testThread, NULL); 

應該

ret = pthread_create(&pTestThread, NULL, testThread, NULL); 
+0

我的意思是,在這個例子中,*技術*'(void *)testThread'沒有錯......它只是嘗試通過強制轉換函數的返回類型。 – tonysdg

+0

我發佈的示例代碼在其他平臺(如arm或windows linux)中運行良好。但只有它在linux 2.6.21&mips平臺上遇到麻煩。 –

0

你的代碼來看, (假設main_loop和句柄是全局的)

if (main_loop == 1){ // Main thread will exit for if 1st argument is 1 
     exit(0); 
     } 
     else if (main_loop == 2) // Main thread will exit for if 1st argument is 2 
     { 
     sleep(10); 
     exit(0); 
     } 
     else if (main_loop == 3) // Main thread should not exit if 3 
     { 
     pthread_exit(NULL); 
     } 
     else if (main_loop == 4) 
     { 
     // Main thread should not exit if 3, delay however is 10 seconds 
     sleep(10); 
     pthread_exit(NULL); 
     } 
    // Interestingly for all other values main should run as usual 

嘗試運行的二進制文件,如

./a.out 3 1 
./a.out 4 1 
./a.out 123 1 

注 - 第二個參數是始終爲1,進行測序執行。

相關問題