为什么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 */  

为什么将errno从全局变量替换成函数后就是线程安全了呢?

阅读 5.6k
2 个回答

各个线程访问的不是同一个值,将各个线程的errno存为线程私有数据阿什么的就行了,总之避免访问一个就好了
单线程的话就直接一个就行了。。

利用线程全局变量实现

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进