linux之errno与线程安全

在linux或者unix环境中,errno是一个十分重要的部分。在调用的函数出现问题的时候,我们可以通过errno的值来确定出错的原因,这就会 涉及到一个问题,那就是如何保证errno在多线程或者进程中安全?我们希望在多线程或者进程中,每个线程或者进程都拥有自己独立和唯一的一个 errno,这样就能够保证不会有竞争条件的出现。一般而言,编译器会自动保证errno的安全性,但是为了妥善期间,我们希望在写makefile的时 候把_LIBC_REENTRANT宏定义,比如我们在检查<bits/errno.h>文件中发现如下的定义:
# 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 */
也就是说,在没有定义__LIBC或者定义_LIBC_REENTRANT的时候,errno是多线程/进程安全的。
一般而言, __ASSEMBLER__, _LIBC和_LIBC_REENTRANT都不会被编译器定义,但是如果我们定义_LIBC_REENTRANT一次又何妨那? <!--[if !vml]--><!--[endif]-->
为了检测一下你编译器是否定义上述变量,不妨使用下面一个简单程序。
#include <stdio.h>
#include <errno.h>

int main( void )
{
#ifndef __ASSEMBLER__
        printf( "Undefine __ASSEMBLER__\n" );
#else
        printf( "define __ASSEMBLER__\n" );
#endif

#ifndef __LIBC
        printf( "Undefine __LIBC\n" );
#else
        printf( "define __LIBC\n" );
#endif

#ifndef _LIBC_REENTRANT
        printf( "Undefine _LIBC_REENTRANT\n" );
#else
        printf( "define _LIBC_REENTRANT\n" );
#endif

        return 0;
}
希望读者在进行移植的时候,读一下相关的unix版本的<bits/errno.h>文件,来确定应该定义什么宏。不同的unix版本可能存在着一些小的差别!
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->


---------------------------------此外---------------------------------------

使用gcc -D_THREAD_SAFE,此时errno是每个线程相关的,不再是全局变量。当正确地

包含<errno.h>之后,errno被重新定义过:

#if defined(_THREAD_SAFE) || defined(_THREAD_SAFE_ERRNO)
/*
 * Per thread errno is provided by the threads provider. Both the extern
 * int and the per thread value must be maintained by the threads library.
 */
#define errno (*_Errno())

#endif

extern int errno;

函数_Errno()返回一个指针,指向一个线程相关整数。注意,你仍然可以使用&errno。

在/usr/include/*.h中grep查看一下,会发现如果指定了-D_THREAD_SAFE,就不必再
指定-D_REENTRANT以及-D_THREAD_SAFE_ERRNO。

猜你喜欢

转载自blog.csdn.net/iot_shun/article/details/80985257
今日推荐