About the volatile modifier

Source: Internet
Author: User
Tags modifier volatile

This summary is currently doing knowledge, I have not done too many related practical applications.


1. Overview

Just like the more familiar const, volatile is a type modifier (kind specifier). It is designed to modify variables that are accessed and modified by different threads. If there is no volatile, it will basically lead to the result that either the multithreaded program cannot be written, or the compiler loses the opportunity for a lot of optimizations.

2. In depth

The following is the information in Baidu Encyclopedia:

Recommending a variable that is defined as volatile is that the variable may be unexpectedly altered so that the compiler does not assume the value of the variable. Precisely, the optimizer must carefully reread the value of the variable every time it uses it, instead of using a backup stored in the register. Here are a few examples of volatile variables:

1. Hardware registers for parallel devices (e.g., status registers)

2). Non-automatic variable (non-automatic variables) that is accessed in an interrupt service subroutine

3. Variables shared by several tasks in multi-threaded applications

A person who cannot answer this question will not be hired.

I think this is the most basic problem to differentiate between C programmers and embedded system programmers. Embedded system programmers often deal with hardware, interrupts, RTOs, and so on, which require volatile variables. Not knowing volatile content will bring disaster.

Assuming the interviewer has answered the question correctly (well, doubt it will be), I'm going to delve a little deeper and see if this guy really understands volatile's full importance.

1. Can a parameter be either a const or a volatile? explain why.

2). Can a pointer be a volatile? explain why.

3). What's wrong with the following function:

int square (volatile int *ptr)

{

return *ptr * *PTR;

}

here is the answer:

1). Yes. An example is a read-only status register. It is volatile because it can be changed unexpectedly. It is const because the program should not attempt to modify it.

2). Yes. Although this is not very common. An example is when an interrupt service subroutine modifies a pointer to a buffer.

3). This code is a prank. The purpose of this code is to return the pointer *ptr to the square of the value, but since *ptr points to a volatile parameter, the compiler will produce code similar to the following:

int square (volatile int *ptr)

{

int a,b;

A = *ptr;

b = *ptr;

return a * b;

}

Because the value of the *ptr can be changed unexpectedly, A and B can be different. As a result, this code may not return the square value you expected. The correct code is as follows:

Long Square (volatile int *ptr)

{

int A;

A = *ptr;

Return a * A;

}

Understand

1. Compiler optimization (please help me to see the following understanding)

In this thread, when reading a variable, in order to improve access speed, compiler optimization sometimes read the variable to a register, and then take the variable value, directly from the register value;

When the value of the variable changes in the Chengri, the new value of the variable is also added to the register to keep the same.

When a variable changes a value because of another thread, the value of the register does not change, causing the application to read a value that is inconsistent with the actual variable value.

When the register changes values because of another thread, the value of the original variable does not change, causing the application to read a value that is inconsistent with the actual variable value.


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.