LeaveCriticalSection()是否将缓存的变量刷新到内存中? [英] Does LeaveCriticalSection() flushes the cached variables to memory?
问题描述
说我有以下代码:
// global variable
int i = 0;
...
EnterCriticalSection(&CriticalSection);
i = 45;
i = 24;
i = 32;
LeaveCriticalSection(&CriticalSection);
CPU是否可以缓存变量i
,并且在修改i
时,已缓存的副本是已修改的副本,并且在调用LeaveCriticalSection()
时,会将i
的缓存值刷新到内存中(因此例如其他线程可以看到更新后的值)?
Can the CPU cache the variable i
, and when i
is modified, the cached copy is the one modified, and when LeaveCriticalSection()
is called, the cached value of i
is flushed to memory (so that for example other threads can see the updated value)?
推荐答案
根据
以下同步功能使用适当的屏障来确保内存顺序:
The following synchronization functions use the appropriate barriers to ensure memory ordering: 这并不一定意味着数据被丢弃到内存".确实,您可能会认为某些缓存是内存的一部分,而其他缓存可能与单个CPU相关联. 适当的障碍"会为机器体系结构做任何必要的工作,以确保所有CPU都具有一致的视图. This doesn't necessarily mean that data is "flushed to memory". Indeed, you might think of certain caches being part of the memory, while other caches might be associated with a single CPU. The "appropriate barriers" do whatever is necessary for the machine architecture to guarantee that all CPUs have a consistent view. 有关更多信息,您可能想从内存壁垒上的Wikipedia页面开始. >和内存排序. For more information, you might want to start with the Wikipedia pages on memory barriers and memory ordering. 这篇关于LeaveCriticalSection()是否将缓存的变量刷新到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!