2017-08-08 68 views
0

我目前正在使用pthread並從這裏閱讀文檔:Pthread Manual Pthread Join如何閱讀POSIX的手冊頁引用錯誤代碼?

但是,當我讀取頁面時,我看到錯誤,但沒有返回相應的返回值,這將從pthread_join返回。所以我的問題是,ERRORS是按升序排列的(因爲它可能是一個枚舉器)?

+0

不,你不能假設有關規定值什麼,只知道他們是INT-兼容,並且從來沒有在成功的情況下,對應於任何可能的返回值。 –

回答

1

錯誤值(如EDEADLK)是通過包含<errno.h>頭文件定義的宏。

實施例:

#include <pthread.h> 
#include <errno.h> 

... 

int retval = pthread_join(threadID, NULL); 
if (retval == EDEADLK) 
{ 
    // error-handling code for deadlock 
} 
else if (retval == EINVAL) 
{ 
    // error-handling code for invalid thread id 
} 
else if (retval == ESRCH ) 
{ 
    // error-handling code for no such thread id 
} 

注意的是,上述代碼是正確僅限Linux,作爲唯一的錯誤數specified by POSIX for pthread_join()EDEADLCK

the POSIX standard for error numbers,(粗體部分以您的問題特別相關):

2.3錯誤編號

大部分功能可以提供一個錯誤號。每個 函數提供其錯誤編號的方式在其說明中指定。

一些功能可以提供通過 訪問的符號errno,通過包括<errno.h>頭中定義的變量的錯誤編號。 只有在指示 通過函數的返回值有效時才應該檢查errno的值。本卷中的功能 POSIX.1-2008中的任何功能都不應將errno設置爲零。對於進程的每個線程, errno的值不應受其他線程的函數調用或對errno的分配的影響。

某些函數直接返回錯誤號作爲函數值。這些函數返回值爲零以指示成功。

如果在處理一個函數調用中出現一次以上的錯誤,可能出現的錯誤中的任一個 可以返回,作爲檢測順序是 未定義。

實現可以支持不包括在此名單 其他錯誤,可能會產生包括在情況下 比這裏所描述的,其他該列表錯誤或可能包含擴展或 限制,以防止一些錯誤的發生。

每個參考頁面上的錯誤部分指定哪些錯誤 條件由所有實現被檢測(「應失敗」),並且可以由實現方式被任選地檢測 (「可能會失敗」)。如果 未檢測到錯誤情況,則請求的操作應爲 成功。如果檢測到錯誤情況,除非另有說明,否則可能已部分執行了要求的動作 。

實現可以產生下不同於所描述 情況這裏列出錯誤編號,當且僅當所有這些 錯誤條件總是可以被同樣地處理到誤差 條件如在本體積POSIX.1-2008的說明。 對於本卷的POSIX.1-2008中描述的錯誤條件 ,實現不應從本卷所需的一個 生成一個不同的錯誤編號,但可能會生成其他 錯誤,除非明確禁止某個特定錯誤功能。

每個實現應記錄,一致性文檔中, 情況,其中每個任選的條件 POSIX.1-2008檢測定義。一致性文檔還可能包含 聲明,表示檢測到一個或多個可選錯誤條件不是 。

某些與線程相關的函數不允許返回[EINTR]的代碼錯誤 。在適用的情況下,在個別功能頁面的錯誤部分 中進行說明。

以下宏名稱標識可能的錯誤編號,在本卷中專門定義的功能的上下文中爲POSIX.1-2008;這些一般描述在 中返回它們的函數的ERRORS部分更加精確地定義。由於 錯誤編號的實際值未指定,因此只應在程序中使用這些 宏名稱。本節中列出的所有值應爲 唯一,除非如下所述。所有這些宏 的值應在POSIX.1-2008的基礎 定義卷中定義的<errno.h>標題中找到。本卷的POSIX.1-2008未指定實際值 。

[E2BIG] 
    Argument list too long. The sum of the number of bytes 
    used by the new process image's argument list and environment 
    list is greater than the system-imposed limit of {ARG_MAX} bytes. 

    or: 

    Lack of space in an output buffer. 

    or: 

    Argument is greater than the system-imposed maximum. 
[EACCES] 
    Permission denied. An attempt was made to access a file 
    in a way forbidden by its file access permissions. 
[EADDRINUSE] 
    Address in use. The specified address is in use. 
[EADDRNOTAVAIL] 
    Address not available. The specified address is not 
    available from the local system. 
[EAFNOSUPPORT] 
    Address family not supported. The implementation does 
    not support the specified address family, or the specified 
    address is not a valid address for the address family 
    of the specified socket. 
[EAGAIN] 
    Resource temporarily unavailable. This is a temporary 
    condition and later calls to the same routine may complete 
    normally. 
[EALREADY] 
    Connection already in progress. A connection request is already 
    in progress for the specified socket. 
    . 
    . 
    .