Detailed Explanation of C Language Keywords (5) Take you to a comprehensive understanding of the volatile keyword

I. Introduction

Hello everyone, welcome to the C language in-depth analysis column - C language keyword detailed explanation fifth, in this article we will introduce another important keyword in the C language volatile, I believe everyone after reading this blog Have a systematic and comprehensive understanding of the usage and precautions of the volatile keyword.

Second, the most volatile keyword - volatile

Volatile means volatile and unstable. Many people have never seen this keyword at all and do not know it exists. There are also many programmers who know it exists but never use it. It is precisely because it is unpopular that when interviewing questions related to C language, volatile, static and const have become the most frequently asked questions.

1. General description of volatile

The volatile keyword, like const, is a type modifier, and the variable it modifies can be changed by some compilers, unknown factors, such as the operating system, hardware, or other threads. When encountering a variable declared with this keyword, the compiler will no longer optimize the code that accesses the variable, thus providing stable access to special addresses. This is the explanation of volatile in C in-depth analysis. Now we may not be interested in this description. It doesn't matter. We will look back at it after we introduce volatile in detail. have a different feeling.

2. CPU operation and memory coverage

Before introducing volatile, we need to introduce the concepts related to CPU operation and memory coverage. insert image description here>As shown in the figure: Here we mark a loop with flag. When the compiler executes this statement, it needs the CPU to participate in the logical judgment of the loop. When the CPU makes logical judgment, it first loads the variable flag into the register, and then Determine whether the loop condition is true, and then execute the loop statement if it is true, but there is nothing here that can modify the value of my loop variable flag, that is, we define an infinite loop, then, in order to continue this loop , the CPU needs to continuously load the variable flag from the memory into the register for logical judgment. Obviously, this is very inefficient. Therefore, in order to improve the efficiency, the CPU will directly put the flag in the register. The value of the flag is read from the register and is no longer read from the memory. This situation is also called "memory overwrite".

3. Threads and execution flow

From our current point of view, this approach is obviously not a problem, because the code we write now is single-threaded, and there is only one execution flow to execute the program, but a program is not only single-threaded, when we learn in the future When you use multi-threaded and multi-process code, you will know that the loop variable flag may be modified by other values ​​outside the while. When a logic that exists in parallel with while changes the flag to 0, the problem comes, because the CPU is The value of the flag is directly read from the register for the logical judgment of the while loop, so when another logic changes the flag to 0, the while loop will not stop, but will continue to execute the code block in it, causing the program logic (you only need to know about multi-threading and multi-process knowledge now, and you will study it in detail later).

4. volatile modified variable

So, in order to have the above problems in some special cases, what should we do? We can directly add the volatile keyword in front of the flag variable, so that the CPU does not optimize the flag, and continues to read the value of the flag from the memory every time.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
    
    
	volatile int flag = 1;
	while (flag) {
    
    
		;
	}
	return 0;
}

This is the role of the volatile keyword modified variable: let the compiler not optimize the variable modified by volatile, so as to achieve the purpose of stable access to memory.
Note: Although volatile is called a volatile keyword, this is only to describe that the variable it modifies may change. The compiler should pay attention to it, it does not require that the variable modified by it must
change! Pay special attention to this.

3. Summary

  • In order to improve the operating efficiency of the compiler when running the program, it may optimize some programs, which may directly place some variables that need to be read from memory repeatedly in the register (this can be done with register. Contrast), resulting in the problem of program logic errors in the case of multi-threading. In order to solve or avoid such errors, the volatile keyword is defined in C language.
  • Variables modified by the volatile keyword can be changed by some compiler, unknown factors, such as the operating system, hardware, or other threads. When encountering a variable declared with this keyword, the compiler will no longer optimize the code that accesses the variable, thus providing stable access to special addresses.

More keywords are in the blog link below.
Detailed explanation of C language keywords (1) auto, register keyword
detailed explanation of C language keywords (2) to give you a comprehensive understanding of static
C language keyword detailed explanation (3) data type and sizeof keyword
C language Keyword Detailed Explanation (4) Take you to a comprehensive understanding of the const keyword

If you think this article is helpful to you, please give a triple support

Guess you like

Origin blog.csdn.net/m0_62391199/article/details/123746218