2009-04-10 44 views
1

pthread_create返回值251而不創建線程。有誰知道問題是什麼?請幫忙。該機器是HP-UX。pthread返回251

我是新來的多線程。

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

    void *print_message_function(void *ptr); 

    main() 
    { 
     pthread_t thread1, thread2; 
     char *message1 = "Thread 1"; 
     char *message2 = "Thread 2"; 
     int iret1, iret2; 
     /* Create independent threads each of which will 
     * execute function */ 

     iret1 = pthread_create(&thread1, NULL, print_message_function, (void*) message1); 
     iret2 = pthread_create(&thread2, NULL, print_message_function, (void*) message2); 

     /* Wait till threads are complete before 
     * main continues. Unless we */ 
     /* wait we run the risk of executing an 
     * exit which will terminate */ 
     /* the process and all threads before the 
     * threads have completed. */ 

     pthread_join(thread1, NULL); 
     pthread_join(thread2, NULL); 
     printf("Thread 1 returns: %d\n",iret1); 
     printf("Thread 2 returns: %d\n",iret2); 
     exit(0); 
    } 

    void *print_message_function(void *ptr) 
    { 
     char *message; 
     message = (char *) ptr; 
     printf("%s \n", message); 
    } 

回答

4

編輯:在HP-UX11上。 pthread_create失敗,錯誤251:函數不可用。

檢查鏈接順序中是否存在-lc之前的-lpthread。 如果是這種情況,那麼該調用將解析爲存儲在C庫 中,並可能導致此錯誤。


你鏈接到-lpthread嗎?

您應該使用errno.h中看到251是您的系統上有什麼錯誤,或者這應該給你一個更的相關詳細信息:

printf("%s\n", strerror(errno)); 

此外,使用並行線程的時候,你應該檢查返回值幾乎每次調用並行線程*(參見各功能的man檢查返回的可能的錯誤)

對於在pthread_create,你至少有2個可能出現的錯誤(取決於您的系統和並行線程的實現上):

[EAGAIN該系統缺乏必要的資源來創建 另一個線程,或系統強加的限制上的螺紋的 總數在工藝 [PTHREAD_THREADS_MAX]將超過:如果10在pthread_create()將失敗。

[EINVAL] attr指定的值無效。

0

這編譯和運行在我的Linux機器,結果如下:

Thread 1 
Thread 2 
Thread 1 returns: 0 
Thread 2 returns: 0 

所以看起來這個問題是不是在你的代碼,但在一些環境。我有10多年沒有使用過HP-UX,所以我無法在那幫助你。

+0

是的,看起來它跟環境有關。 – Shree 2009-04-10 09:15:03

+0

似乎它可能是鏈接順序,請檢查我編輯的答案。 – claf 2009-04-10 09:16:08