觀點(diǎn)一:
errno是個(gè)全局變量,應(yīng)該不是線程安全的吧.
觀點(diǎn)二:
可以的。
extern int*__errno_location(void)
#define errno (*__errno_location())
保證自己維護(hù)一個(gè);
觀點(diǎn)三:
沒問題,線程之間不影響,實(shí)際errno是一個(gè)函數(shù);
觀點(diǎn)四:
線程對(duì)信號(hào)的反應(yīng),會(huì)隨系統(tǒng)不同而有些差異,并不是全部按照posix標(biāo)準(zhǔn)(記得早期的posix標(biāo)準(zhǔn)在這一點(diǎn)上似乎也比較模糊),在redhat linux7.2上,發(fā)送給進(jìn)程的信號(hào),會(huì)被發(fā)送給進(jìn)程內(nèi)所有的線程,而在solaris和redhat9上,只有主線程才接受到發(fā)給進(jìn)程的信號(hào)另外,要當(dāng)心在多線程應(yīng)用中error變量只有在定義了宏_REENTRANT后,才會(huì)有如下的定義出現(xiàn):
int * __errno_location();
#define errno (*__errno_location())
否則,errno只是一個(gè)非線程安全的全局變量,因此perror也將沒有準(zhǔn)確的含義。
我自己在虛擬機(jī)上安裝的Fedora 5中用man errno得到如下內(nèi)容,請(qǐng)大家認(rèn)真閱讀:
The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong. Its value is significant only when the call returned an error (usually -1), and a function that does succeed is allowed to change errno. Sometimes, when -1 is also a valid successful return value one has to zero errno before the call in order to detect possible errors.
errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro. errno is thread-local; setting it in one thread does not affect its value in any other thread.
Valid error numbers are all non-zero; errno is never set to zero by any library function. All the error names specified by POSIX.1 must have distinct values, with the exception of EAGAIN and EWOULDBLOCK, which may be the same.
至于什么是thread_local,下面給出一段Windows上TLS的定義供參考學(xué)習(xí):
Thread local storage (TLS) is the method by which each thread in a multithreaded process allocates a location in which to store thread-specific data.
我的理解就是每個(gè)線程都有一個(gè)自己的errno,互相之間不影響。其實(shí)等于沒有說,setting it in one thread does not affect its value in any other thread 已經(jīng)說的夠清楚了。呵呵!