errno is thread-safe under LINUX __linux

Source: Internet
Author: User
errno is thread-safe under Linux 2011-06-20 11:34:54

Category: Linux in Linux or UNIX environments, errno is a very important part. When there is a problem with the called function, we can determine the cause of the error by errno value, which involves a problem, that is, how to ensure that the errno in multithreading or process security. We want each thread or process to have its own independent and unique errno in multiple threads or processes, so that there is no competitive condition to emerge. In general, the compiler automatically guarantees errno security, but in order to do so, we want to define the _libc_reentrant macro when writing makefile, for example, we found the following definition in the check 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/*!__assembler__ *
#endif/* _errno_h * *
That is, errno is multithreaded/process-safe when there is no definition of __libc or definition of _libc_reentrant.
Generally speaking, __assembler__, _LIBC, and _libc_reentrant are not defined by the compiler, but if we define _libc_reentrant once it doesn't hurt that.
To check whether your compiler defines the above variables, you might want to use one of the following simple programs.
#include
#include

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;
}
Hopefully, when the reader is porting, read the relevant UNIX version of the file to determine what macros should be defined. There may be some small differences in the different versions of UNIX.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.