errno变量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoyilong2007101095/article/details/83055767

linux中的errno是个线程全局变量,每个线程都维护了一个errno,但是怎么实现的呢?

查看了errno.h头文件,看到各种宏,然后又include了<bits/errno.h>,里面又有define errno (*__errno_location ()),又是各种宏包围着,完全不知道该如何找?各位有没有好的办法推荐下

最后采用了gcc的预编译选项gcc -E ***,查看预编译过的文件,看到errno变成了int a = (*__errno_location ());

这说明起作用的是define errno (*__errno_location ()),查看官方文档:

__errno_location

Name

__errno_location -- address of errno variable

Synopsis

int * __errno_location(void);

Description

The __errno_location() function shall return the address of the errno variable for the current thread.

__errno_location() is not in the source standard; it is only in the binary standard.

内部实现目前没有看到源码,也查了一些其它博客,应该是用

  void *pthread_getspecific(pthread_key_t key);
  int pthread_setspecific(pthread_key_t key, const void *value);

这一族函数实现的,具体的请各位知道的评论里面说下

猜你喜欢

转载自blog.csdn.net/xiaoyilong2007101095/article/details/83055767