From Linux errno to Java threadlocal

Source: Internet
Author: User

The errno in Linux

When executing system calls under Linux, a return value typically indicates success or failure, but this value only indicates success or failure, but does not indicate how successful or failed.

Errno is to solve this problem, the system call will set the error number to errno, we can use the error number to know the reason for the failure. You can also use Strerror to print out the string description for this error number. The old version of Linux needs to add the extern int errno, which is now directly introduced into <errno.h>.

errno Example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/6A/wKioL1WbuPjQ9_gzAAHrJFspGzI545.jpg "title=" Selection _ 002.png "alt=" Wkiol1wbupjq9_gzaahrjfspgzi545.jpg "/>

Now the question is coming. If I have two threads, the code is as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/6D/wKiom1Wbt2CSHc8DAAC4FjSCW0c854.jpg "title=" Selection _ 011.png "alt=" Wkiom1wbt2cshc8daac4fjscw0c854.jpg "/>

errno is a global variable, then if the execution order is 1-1,2-1,1-2,2-2, thread 1 prints out the errno is thread 2, this is errno problem, thread is unsafe.

But the tests were many times and the results were correct. Although you use this errno as a global variable, it is actually a macro. Using GCC-E to expand the source code macro, you can see that errno is replaced with a function.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/6A/wKioL1WbuZuyhJVTAAJJJiKqJHo234.jpg "title=" Selection _ 003.png "alt=" Wkiol1wbuzuyhjvtaajjjikqjho234.jpg "/>

Using man errno to view the explanation of errno, you can see the following sentence:

errno is defined by the ISO C standard to be a modifiable lvalue of
type int, and must not being explicitly declared; errno may be a macro.
Errno is thread-local; Setting it in one thread does not affect it
value in any other thread.

So errno is not a global variable, but a thread-local, each thread has one. We can implement a errno ourselves.


Write yourself a errno

Just demonstrate the fundamentals of errno and use a map to store the errno of each thread, and this map key is ThreadID. When the system call finishes executing, the error code is written to the corresponding errno of this thread. The system call in the code is a fake system call, Some_system_call ().

In the actual development, for the developer, he actually does not see Set_errno (), Get_errno () These functions, he only need to take errno on the line.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/6D/wKiom1Wbudzhz-3IAAUruAEgEOY812.jpg "title=" Selection _ 007.png "alt=" Wkiom1wbudzhz-3iaauruaegeoy812.jpg "/>


Java's threadlocal

Java's threadlocal and Linux errno are the same, but errno is a bit simpler for developers, and can only be taken as a variable.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/6A/wKioL1WbvwTCsMG7AAI8sT9dyJo690.jpg "title=" Selection _ 013.png "alt=" Wkiol1wbvwtcsmg7aai8st9dyjo690.jpg "/>

Only one threadlocal is defined, but each line thread is different.

Implement a threadlocal yourself

In fact, Threadlocal also and above my own implementation of the errno similar, with a map, interested can go to see the JDK source code, I wrote a very simple mythreadlocal,java threadlocal principle is roughly the case. The threadlocal of the above code can be replaced directly by Mythreadlocal to run, of course, this mythreadlocal is too simple, just to introduce the principle.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/6A/wKioL1WbwHKxdsl-AAJ3jZYOBEA724.jpg "title=" Selection _ 009.png "alt=" Wkiol1wbwhkxdsl-aaj3jzyobea724.jpg "/>


This article from "Niu Blog" blog, reproduced please contact the author!

From Linux errno to Java threadlocal

Related Article

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.