In Linux or UNIX environments, errno is a very important part. When the called function encounters a problem, we can use the errno value to determine the cause of the Error. This will involve a problem, that is, how to ensure the security of errno in multithreading or processes? We hope that in a multi-thread or process, each thread or process has its own independent and unique errno, so as to ensure that there will be no competition conditions. Generally, the compiler automatically guarantees the security of errno. However, we want to define the _ libc_reentrant Macro when writing makefile to ensure the security of errno. For example, we are checking <bits/errno. h> The following definitions are found in the file:
# 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 /*! _ Cycler __*/
# Endif/* _ errno_h */
That is to say, when _ libc or _ libc_reentrant is not defined, errno is multithread/process secure.
Generally, _ javaser __, _ libc and _ libc_reentrant are not defined by the compiler. But what if we define _ libc_reentrant once?
To check whether your compiler defines the preceding variables, use the following simple program.
# Include <stdio. h>
# Include <errno. h>
Int main (void)
{
# Ifndef _ assembler __
Printf ("undefine _ er Er _/N ");
# Else
Printf ("DEFINE _ explorer _/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;
}
We hope that you can read the <bits/errno. h> file of the relevant unix version to determine what macros should be defined. There may be some minor differences between different UNIX versions!