Volatile(关键字):编译器优化、内存屏障与多线程共享内存
文章目录
volatile 关键字向编译器/优化器表明,它应始终对变量或内存执行读取或写入操作,而不将其缓存在本地。它还确保更改的顺序得以保留,不会被优化器改变,并且表面上冗余的代码不会被优化掉。然而,该关键字并不保证内存访问不会在运行时被处理器重新排序。为了解决由此可能产生的问题,请确保使用 原子操作 或 内存屏障 来操作内存。
基础
volatile 关键字可用于类型定义以及函数参数、变量定义和类型转换。与 const 关键字类似,它使变量本身或其指向的数据成为 volatile,或两者皆是。
/* Ensures the changes to/reads from x are always performed */ volatile int x; /* Ensures that changes to/reads from the data that ptr points to (but not the value itself) are always performed */ volatile int *ptr; int volatile *ptr; /* Ensures that changes to/reads from the pointer ptr (but not the data it points to) are always performed */ int * volatile ptr; /* Ensures that changes to/reads from the pointer ptr and also the data it points to are always performed */ volatile int * volatile ptr; int volatile * volatile ptr;
示例
循环
这是一个函数示例,该函数本应产生一个非常短的延迟,但可能被编译器完全优化掉,因为对优化器而言这段代码似乎是冗余的:
static void some_delay(void) { int i; /* Should be declared as '''volatile int i;''' to prevent the loop or even the entire function from being optimized away */ for (i = 0; i < 100; i++) { /* Do nothing, just wait a little while so that some hardware can respond. Using this technique is rarely neccessary and should be avoided! */ } }
此示例本应轮询一个字节,直到其变为 0(例如等待一个 自旋锁 被释放)。显然另一个线程应当更改该内存,但由于编译器对此一无所知,我们需要确保代码在任何情况下都不会被优化掉:
typedef unsigned char * spin_lock; /* Should be defined as '''typedef volatile unsigned char * spin_lock;''' to prevent reading/writing to spin_lock variables being cached in CPU registers! */ static void poll_spinlock(spin_lock lock) { while (*lock != 0) { /* Do nothing, just poll... */ } } /* Alternatively, if spin_lock wasn't defined as a pointer to volatile data you can use it in a typecast instead: */ static void poll_spinlock(spin_lock lock) { while (*(volatile unsigned char *)lock != 0) { /* Do nothing, just poll... */ } }
解引用内存
这是一个代码示例,本应访问一块内存(例如使其变为常驻,或检查其是否有效且不会引发缺页异常):
static void touch_mem(void *ptr) { char *data = (char *)ptr; /* Should be declared as '''volatile char *data = (volatile char *)ptr;''' to prevent the code from being optimized away */ /* It is assumed that the memory is not used by other threads at the same time */ *data = *data; }
另请参阅 APIC 示例代码,其中 volatile 关键字至关重要(读取/写入硬件寄存器)。
假设你有一个线程 "A",它循环运行,直到另一个线程 "B" 希望线程 "A" 优雅地终止。它们共享一个带有 "terminate" 字段的结构体,线程 "A" 轮询该字段以查看是否有其他线程希望它终止。除非该字段或整个结构体被声明为 volatile,否则无法保证代码按预期工作,因为编译器/优化器不知道该字段在循环期间被其他人访问。因此它可能生成只读取一次该值并将其缓存的代码,导致无限循环。使用 volatile 来防止生成错误的代码:
typedef struct { int terminate; /* Should be volatile int terminate; */ } shared_thread_data; /* This is the code run by thread "B" to get thread "A" to terminate gracefully */ void stop_worker_thread(shared_thread_data *thread) { thread->terminate = 1; /* Techni<cally incorrect, you should use a function that guarantees an atomic operation (e.g. lock xchg) } /* This is the thread "A" loop */ void worker_thread_body(shared_thread_data *thread) { while (thread->terminate == 0) { /* Do some work here... */ } }
外部链接
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Volatile (keyword);离线修订号:15381。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论