errno线程安全性

Posted hachikot

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了errno线程安全性相关的知识,希望对你有一定的参考价值。

errno

errno用于获取系统最后一次出错的错误代码。在C++中,errno其实是宏

// windows
#define errno (*_errno())

// linux
#define errno (*__errno_location ())

errno是线程安全的

在C++98中虽然没有规定这一点,但具体实现中基本都是线程安全的,POSIX.1c就规定errno是线程局部的,比如linux中对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 */

在C++11标准中规定了errno是线程局部的。

以上是关于errno线程安全性的主要内容,如果未能解决你的问题,请参考以下文章