The volatile keyword of C++

 In C++, we often use constkeywords to modify a variable to tell the compiler that the variable is immutable. If some code attempts to change the variable, the compilation will fail. In C++, there is another volatilekeyword, which expresses constthe opposite meaning, which is to tell the compiler that volatilethe modified variable is very easy to occur , so as to avoid certain behaviors 变化made by the compiler .优化

Compiler optimization behavior

 Consider the following piece of code

int some_int = 100;

while(some_int == 100)
{
    
    
   //your code
}

 When this code is compiled, if the compiler does not find any code that attempts to modify the behavior of some_int, the compiler will try to perform some 优化behaviors, for example, optimize while(some_int) to while(true), so that the program is executing The time will change faster, because the condition inside the while will always be true when some_int is not changed. If the compiler does not optimize here, the program will 获取go to the value of some_int every time, and then compare it with 100, which will be slower in comparison.

 However, sometimes, compilation optimization is not ours 预期, because there may be some 无法察觉places where the compiler is modifying the variable some_int. In this case, the optimization behavior of the compiler will completely change the behavior of this 本意code 预期外.

《C++ Primer》and 《Effective C++》are essential books for C++ developers. If you want to get started with C++ and want to improve C++ development technology, these two books can be said to be necessary. In addition, 《Linux高性能服务器编程》and 《Linux多线程服务端编程:使用muduo C++网络库》.(陈硕)》is a cheat to quickly improve your linuxdevelopment ability. It takes some effort to search for relevant resources on the Internet. Students who need it can follow the official account 【程序员DeRozan】and reply 【1207】quickly and get it for free~

volatile keyword

 In order to ensure that the program runs as expected, we need to explicitly tell the compiler not to optimize the while loop. At this time, we need to use volatilekeywords, which is volatilethe role of keywords. All we need to do is prepend the variable name volatile:

volatile int some_int = 100; //note the 'volatile' qualifier now!

 A particularly interesting sentence to explain the role of the volatile keyword:

“Hey compiler, I’m volatile and, you know, I can be changed by some XYZ that you’re not even aware of. That XYZ could be anything. Maybe some alien outside this planet called program. Maybe some lightning, some form of interrupt, volcanoes, etc can mutate me. Maybe. You never know who is going to change me! So O you ignorant, stop playing an all-knowing god, and don’t dare touch the code where I’m present. Okay?”

 For a class or structure, if the volatile keyword is used to modify the class object or structure object, the data in the object will be declared as volatile, which is constsimilar to the behavior here.

struct whatever {
    
     int data; };

const whatever test;

Guess you like

Origin blog.csdn.net/dddgggd/article/details/129460814