When we learn the 32 keywords of C language, we don't pay much attention to the keyword volatile. Volatile is a type modifier. The Chinese meaning of volatile is "changeable". So when do we use it in the program? We often see the use of this keyword when analyzing the kernel. For example:
view plaincopy to clipboardprint?
1.struct task_struct {
2.volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
3.void *stack;
4.atomic_t usage;
5.unsigned int flags; /* per process flags, defined below */
6.unsigned int ptrace;
7........
struct task_struct { volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ void *stack; atomic_t usage; unsigned int flags; /* per process flags, defined below */ unsigned int ptrace; .......
In the application, volatile is mainly designed to modify variables that are accessed and modified by different threads.
The volatile variable means that the variable may be changed unexpectedly, so the compiler should not perform corresponding optimizations. To be precise, the optimizer must carefully re-read the value of this variable each time it is used, rather than using the copy stored in the register.
For example:
extern int a; // Statement 1 Assume that a is a global variable
......
int b=a; //Statement 2
int c=a; //Statement 3
The value of a is used in both statement 2 and statement 3. When the optimizer references the value of a in statement 3, it assumes that the value of a has been read into the register in statement 2, and a has not been modified between statement 2 and statement 3. Therefore, there is no need to read the value of a from the memory, so the value in the register is used directly. However, if an interrupt occurs before statement 3 is executed, and the interrupt happens to modify a, then the value retrieved by statement 3 will be problematic.
If the global variable a above is modified as volatile, the optimizer cannot optimize statement 3 and must retrieve the value from the memory.
Previous article:Study Notes - S3C2440 Memory Manager
Next article:Samsung S3C2440 GPIO output driver LED
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- SHT31 Review + My Review Summary
- Using AT89S series microcontroller
- 【McQueen Trial】Use IPAD to program McQueen's car
- The STM32 FFT library calculates the amplitude normally, but the phase is different each time. Has anyone encountered this problem?
- "Show goods" to come to a wave of commonly used development boards
- Measurement of the phase difference between a sine wave and a square wave
- [Sipeed LicheeRV 86 Panel Review] - 6 waft-ui component tests (3)
- Code size after keil compilation
- Tips for removing chip components on PCB
- Hardware System Engineer's Handbook