errno Multithreading Security (reprint)

Source: Internet
Author: User

first, the origin of errno
In C programming, errno is an indispensable variable, especially in network programming. If you have not used errno, it can only indicate that your program is not strong enough. Of course, if you are the GetLastError () of the WIN32 platform, the effect is the same.
Why do you use errno? Personally, this is a system library design in a helpless move, he is more of a skill, rather than architectural needs. We observe the function structure, we can find that the function of the parameter return value only one, this return value can usually carry the error message, such as negative expression error, and positive expression of the correct return value, such as the recv function. But for some functions that return pointers, such as Char *get_str (), this method is obviously not useful. A null can indicate an error, but there is nothing wrong with what has happened. So the errno was born. The global variable errno can store the cause of the error, and when an error occurs, the return value of the function can be an illegal value to indicate that the error occurred.

Second, errno thread safety
errno is a global variable, but in a multithreaded environment, it becomes scary. When you call a function, you find that the function has an error, but when you use the wrong reason, he becomes the error prompt for another thread. It would be a horrible thing to think about.
It's a good idea to set errno as a thread-local variable, in fact, that's how it's done in GCC. He assures that the cause of the errors between threads does not change from one to the other, and when you execute a series of processes serially in a thread, the resulting errno is still correct.
Look at the definition of 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 the threads, errno is a per-thread value. */
# define ERRNO (*__errno_location ())
# endif
# endif/*!__assembler__ * *
This is defined in the errno.h:
/* Declare the ' errno ' variable, unless it ' s defined as a macro by
Bits/errno.h. The case in GNU, where it's a per-thread
Variable. This redeclaration using the macro still works, but it
would be a function declaration without a prototype and may trigger
A-wstrict-prototypes warning. */
#ifndef errno
extern int errno;
#endif
Obviously, errno is actually not what we normally think of as an integer value, but rather an integer pointer to get the value. This integer is thread-safe.


Third, the realization of errno

Static pthread_key_t key;
static pthread_once_t key_once = Pthread_once_init;
static void Make_key ()
{
(void) pthread_key_create (&key, NULL);
}
int *_errno ()
{
int *ptr;
(void) pthread_once (&key_once, Make_key);
if (ptr = pthread_getspecific (key)) = = NULL)
{
ptr = malloc (sizeof (int));
(void) pthread_setspecific (key, PTR);
}
return ptr;
}

iv. Application of errno
Errno is widely used in libraries, but there are actually not so many error encodings. We need to add more error codes to our systems. One way is to use errno directly, another way is to define your own user_errno.
Using errno,strerror may not resolve, which needs to be resolved by itself. But the way errno uses thread variables is worth learning.

errno Multithreading Security (reprint)

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.