linux下errno和windows下getlasterror()


結論是:兩者都是線程安全。

1. getlasterror

參考微軟官方文檔說明如下:

GetLastError function (errhandlingapi.h)
Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code.
...
_Post_equals_last_error_ DWORD GetLastError();
Parameters
This function has no parameters.
...
Return value
The return value is the calling thread's last-error code.

直接說明last-error code變量是線程安全的。

 

2. errno

參考源碼:

# ifndef __ASSEMBLER__
/* Function to get address of global `errno' variable.  */
extern int *__errno_location (void) __THROW __attribute__ ((__const__));

#  if !defined _LIBC || defined _LIBC_REENTRANT
/* When using threads, errno is a per-thread value.  */
#   define errno (*__errno_location ())
#  endif
# endif /* !__ASSEMBLER__ */
#endif /* _ERRNO_H */

注釋寫道,當使用了多線程時(不定義宏_LIBC 或  定義宏_LIBC_REENTRANT),errno是一個線程內變量。這里errno宏代表__errno_location 函數的返回值

在http://www.unix.org/whitepapers/reentrant.html中也發現到:

Redefinition of errno
In POSIX.1, errno is defined as an external global variable. But this definition is unacceptable in a multithreaded environment, because its use can result in nondeterministic results. The problem is that two or more threads can encounter errors, all causing the same errno to be set. Under these circumstances, a thread might end up checking errno after it has already been updated by another thread.

To circumvent the resulting nondeterminism, POSIX.1c redefines errno as a service that can access the per-thread error number as follows (ISO/IEC 9945:1-1996, §2.4):

Some functions may provide the error number in a variable accessed through the symbol errno. The symbol errno is defined by including the header <errno.h>, as specified by the C Standard ... For each thread of a process, the value of errno shall not be affected by function calls or assignments to errno by other threads.
In addition, all POSIX.1c functions avoid using errno and, instead, return the error number directly as the function return value, with a return value of zero indicating that no error was detected. This strategy is, in fact, being followed on a POSIX-wide basis for all new functions.

Footnotes
1.
In POSIX.1c, a "reentrant function" is defined as a "function whose effect, when called by two or more threads, is guaranteed to be as if the threads each executed the function one after another in an undefined order, even if the actual execution is interleaved" (ISO/IEC 9945:1-1996, §2.2.2).
2.
It should be noted that the flockfile() function, like the pthread_mutex_lock() function, can lead to priority inversion. The application developer should take this into account when designing an application and analyzing its performance.

參考https://blog.csdn.net/weixin_35695879/article/details/89530417:

extern __thread int __libc_errno __attribute__ ((tls_model ("initial-exec")));
int * __errno_location (void)
{
  return &__libc_errno;
}

印證了errno是線程安全的

 

p.s.現在使用到的庫函數基本上都是支持POSIX標准的:https://blog.csdn.net/lwwl12/article/details/88763968?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

Linux作為一個從頭開始研制的新操作系統,逐漸發展起來以后為了盡可能獲得大量應用軟件支持,也明智地選擇了用POSIX作為API設計的標准。

linux系統基本都支持POSIX,所以在linux之間,c++程序的移植性是得到保證的

 

參考:https://www.cnblogs.com/zhangyachen/p/10054689.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM