One C volatile per dayEvery day, I picked up a C-language shell, which grew up and formed thousands of miles.
Today's shells: C language volatile is used to avoid errors caused by Compiler optimization.The CPU accesses the memory much faster than the device address, and the access register speed is faster than the memory. To improve the program running speed, the compiler will optimize the program. For example:Buffer = Read (0xEE); // Read port 0xEE data and store it toA = buffer;Buffer=Read(0xEE );// Read port 0xEE data and store it to port B
B = buffer;The compiler may be optimized:Buffer=Read(0xEE );A=Buffer;B=Buffer; // This optimization can reduce one device access.Problem: if the content of port 0 x EE is updated in real time, the optimization will violate the program's original intention. volatile is used to solve this problem, prompting the compiler not to optimize the variable to ensure real-time performance. The usage is as follows:VolatileBuffer=Read(0xEE );Usage: volatile is used to modify variables that may be interrupted or modified by other threads.1. Program interruption2. multi-threaded ProgramVolatile is often used in program design.
Have a nice day!