zoukankan      html  css  js  c++  java
  • 如何让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]-->


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hello_wyq/archive/2006/08/01/1006216.aspx

  • 相关阅读:
    Insus Meta Utility
    The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
    Insus Binary Utility
    asp.net实现文件下载功能
    Column 'Column Name' does not belong to table Table
    程序已被编译为DLL,怎样去修改程序功能
    如何在Web网站实现搜索功能
    如何把数据流转换为二进制字符串
    Asp.net更新文件夹的文件
    如何显示中文月份
  • 原文地址:https://www.cnblogs.com/joeblackzqq/p/2065140.html
Copyright © 2011-2022 走看看