前置知识: C

volatile关键字

1 minAdvanced2026/6/14

C语言volatile关键字作用与使用场景。

1. volatile 的作用

告诉编译器该变量可能被外部因素修改,禁止优化:

// 无 volatile:编译器可能优化掉循环中的读取
int *p = (int*)0x1000;
while (*p) { /* 编译器可能只读一次 */ }

// 有 volatile:每次循环都重新读取
volatile int *p = (volatile int*)0x1000;
while (*p) { /* 每次都从内存读取 */ }

2. 使用场景

2.1 硬件寄存器

volatile uint32_t *GPIO_REG = (volatile uint32_t *)0x40020000;
*GPIO_REG = 0x01;  // 写入硬件寄存器

2.2 中断服务程序共享变量

volatile int flag = 0;

void ISR() {
    flag = 1;  // 中断中修改
}

int main() {
    while (!flag) { /* 等待中断 */ }
}

2.3 多线程共享变量

volatile int shared = 0;
// 注意:volatile 不保证原子性!
// 多线程应使用 atomic 或锁

3. volatile 不保证原子性

volatile int counter = 0;
counter++;  // 不是原子操作(读-改-写三步)
// 应使用 atomic_fetch_add