C ++ keyword volatile Parsing

Source: Internet
Author: User

Volatile is interpreted as "unstable" in English, that is, the value of the variable modified with this keyword may be changed at any time. The type variables declared by the compiler can be changed by unknown factors, such as the operating system, hardware, or other threads. Volatile is intended to be "changeable", but it is more appropriate to translate it into "directly accessing the original memory address. "Easy to change" is caused by external factors, such as multithreading and interruptions. It is not because the variable modified by volatile is "easy to change". If there is no external cause, it is defined by volatile, it will not change either.
During computation in a computer, data is usually copied to the register, because the CPU performs operations on the register much faster than on the memory.
When the variable is not modified using volatile, if no operation is performed on the register where the variable is located during calculation, the computer will think that the value of the variable has not changed, therefore, the system performs operations directly from the register value instead of from the memory/variable source address.
However, if the variable uses the volatile keyword, it indicates that the value of the variable may be changed at any time outside the scope recognized by the compiler (for example, being changed in the terminal or multitasking or multithreading ). At this time, the volatile keyword is used to tell the compiler that it is not allowed to make the assumption that the value of the variable is not changed. Each calculation is performed with the new value from the source address of the variable, instead of copying data stored in registers.
The syntax of the volatile keyword is the same as that of the const keyword.
In short, the keyword volatile is used to cancel any optimization of the variable to be modified by the compiler. Each time a variable is operated, it is read and written from its source address, instead of reading data from the register where the data is saved, the compiler will cancel the optimization of this variable.
The following are examples:
Example 1,
Void delay (int time)
{
For (; time> 0; time --);
}
The above code is intended to implement an inaccurate delayed operation. Generally, there is no problem. But to optimize the code, the function will be optimized after the compiler optimization function is enabled. The compiler finds that this function does not actually do anything. To improve the performance, the compiler will optimize this code.
To prevent being optimized by the compiler, this code can be changed to the following:
Volatile void delay (int time) // The keyword volatile is used here to tell the compiler not to talk about code optimization.
{
For (; time> 0; time --);
}
Example 2,
Float square (volatile float * ptr)
{
Return * ptr ** ptr;
}
The problem with this Code is that the parameter is modified with the volatile keyword, which may lead to the unexpected change of * ptr value during the function call. In fact, the process of this Code is as follows: www.2cto.com
Float square (volatile float * ptr)
{
Float a, B;
A = * ptr; // After assigning a value to a, if the * ptr value is changed when the program is interrupted, the value of B is different from that of.
B = * ptr;
Return a * B;
}
The above code should be changed:
Float square (volatile float * ptr)
{
Float a, B;
A = * ptr;
Return a *;
}
Generally, volatile is used in the following areas:

Volatile must be added to the variable modified in the interrupt service program for other programs to detect;
Volatile should be added to the labels shared by all tasks in a multi-task environment;
Volatile is also required for the hardware registers mapped to memory, because each read/write operation may have different meanings.
In addition, in the above situations, we often need to consider data integrity at the same time (some of the correlated symbols have been read in half and are interrupted for rewriting). In 1, we can achieve this through Guanzhong disconnection, task Scheduling can be disabled in 2, and 3 can only rely on the good design of hardware.

Author: qingtingchen1987
 

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.