Volatile variable 1

Source: Internet
Author: User

Understand volatile Variables
A variable defined as volatile means that this variable may be unexpectedly changed, so that the compiler will not assume the value of this variable. Precisely, the optimizer must carefully re-read the value of this variable every time when using this variable, rather than using the backup stored in the register. The following are examples of volatile variables:
1). Hardware registers of parallel devices (for example, Status Registers)
2) An Interrupted service subaccountProgramNon-automatic variables that will be accessed in)
3) variables shared by several tasks in multi-threaded applications


Maybe you still don't quite understand what volatile variable is. Let's take a look at the following program errors first?
Int square (volatile int * PTR)
{
Return * PTR ** PTR;
}

This section Code It is a prank. The purpose of this Code is to return the pointer * PTR points to the square of the value. However, since * PTR points to a volatile parameter, the compiler will generate code similar to the following:
Int square (volatile int * PTR)
{
Int A, B;
A = * PTR;
B = * PTR;
Return a * B;
}
* The value of * PTR may be unexpectedly changed, so a and B may be different. As a result, this Code may not return the expected square value! The correct code is as follows:
Long square (volatile int * PTR)
{
Int;
A = * PTR;
Return a *;
}
For example, in a thread, when reading a variable, in order to improve the access speed, the compiler sometimes reads the variable to a register during optimization. Later, when getting the variable value, the value is taken directly from the register. When the variable value changes in the local thread, the new value of the variable is copied to the register at the same time, so as to maintain consistency when the variable changes the value due to other threads, the value of this register will not change accordingly, as a result, the value read by the application is inconsistent with the actual variable value. When the value of this register is changed due to other threads, the value of the original variable will not change, as a result, the value read by the application is inconsistent with the actual variable value. In this case, the variable must be declared as volatile.

Here is an inaccurate example:
When paying, the accountant calls the employee to register their bank card number every time; once the accountant did not register immediately for convenience, he used the bank card number previously registered; an employee's bank card has been lost and the bank card number has been reported; as a result, the employee cannot receive the salary
In the following cases, variables often need to be declared as volatile:
1). Hardware registers of parallel devices (for example, Status Registers)
2). Non-automatic variables that will be accessed in an interrupt service subroutine)
3) variables shared by several tasks in multi-threaded applications
 
In short, the volatile keyword is a type modifier. The type variables it declares can be changed by unknown factors in some compilers, such as operating systems, hardware, or other threads. When the variable declared by this keyword is encountered, the compiler will not optimize the code that accesses the variable, so as to provide stable access to the special address. Volatile should be interpreted as "direct access to the original memory address", which is a bit misleading; "easy to change" is caused by external factors, such as multithreading and interruption, it is not because the variable modified with volatile is "changeable". If there is no external cause, it will not change if it is defined with volatile;
With the definition of volatile, this variable will not change because of external reasons and can be safely used.
Reprinted: http://blog.chinaunix.net/u3/104943/showart_2085678.html

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yel617/archive/2010/03/08/5355740.aspx

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.