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是线程局部的。

猜你喜欢

转载自www.cnblogs.com/HachikoT/p/12129076.html