首页 > 为什么errno线程安全?

为什么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从全局变量替换成函数后就是线程安全了呢?


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


利用线程全局变量实现

【热门文章】
【热门文章】