据我所知,编译器(软件)和 CPU(硬件)会出于性能原因对指令进行重新排序,而内存错误可以防止重新排序,它们处于编译器级别或 CPU 级别。
MSDN 说“Interlockedxxxx 函数生成一个完整的内存屏障(或栅栏)以确保内存操作按顺序完成”,我不知道“一个完整的内存屏障”是指硬件屏障还是软件屏障?
boost::atomic 做了什么?硬件障碍?刷新 CPU 缓存/存储缓冲区?
memory_order_acquire 语义使软件或硬件更加混乱?
据我所知,编译器(软件)和 CPU(硬件)会出于性能原因对指令进行重新排序,而内存错误可以防止重新排序,它们处于编译器级别或 CPU 级别。
MSDN 说“Interlockedxxxx 函数生成一个完整的内存屏障(或栅栏)以确保内存操作按顺序完成”,我不知道“一个完整的内存屏障”是指硬件屏障还是软件屏障?
boost::atomic 做了什么?硬件障碍?刷新 CPU 缓存/存储缓冲区?
memory_order_acquire 语义使软件或硬件更加混乱?
它提供指定的内存模型,使用您正在使用的平台所需的任何屏障。
请记住,这boost::atomic
是一个在许多不同平台上实现的可移植 API。另一方面,Windows 桌面 API 仅在 Intel 上运行。因此,MSDN 文档将包含特定于平台的信息(因为只有三个平台——x86、x64、Itanium)而atomic
文档没有。
(注意:大多数 Win32 也用于 ARM 处理器上的 Windows CE,但有一组并行的文档页面。桌面页面不涵盖 ARM。涵盖 Windows 8 RT 的“Windows Store”API 是另一组文档。)
做什么的
boost::atomic
?硬件障碍?刷新 CPU 缓存/存储缓冲区?
有关系吗?
原子操作建立同步关系。他们做任何需要在目标机器上做的事情,以实现由这些关系定义的行为。
例如,具有存储-释放语义的操作与具有加载-获取语义的操作同步。如果操作 A与操作 B 同步,则称 A线程间发生在B 之前,这意味着 A发生在B 之前,这意味着 A 和 B 不会导致数据竞争。(同步关系名称用重点标注)
There are three issues involved when modifying values that are shared between threads. First, there's the possibility of a thread switch in the middle of reading or writing the value, with the result that some other thread could see a partially written (i.e., nonsensical) value. Second, each processor has its own data cache, so writes from one thread on one processor might not be visible to another thread running on another processor. Third, the compiler can reorder instructions, within limits, to make code more efficient. std::atomic
eliminates all three possibilities: reads and writes of atomic objects are done without interruption; writing to an atomic type flushes the cache to main memory, and reading from an atomic type reloads the cache from main memory; and the compiler is not allowed to move instructions across operations on atomic types. The details of how these things are done (including whether there needs to be anything done) depend on the target platform, but that's all done in the standard library's implementation. EDIT: whoops, just noticed that the question asks about Boost. I haven't dug into it, but I assume Boost satisfies the same constraints as the standard library, and implements those constraints appropriately.