内存屏障似乎不影响编译器怎么编译吧;
但如果执行到 mov eax, a 之后再发生 a = 10, wmb(), 那么只会导致CPU 高速缓存失效, 内存被更新; 但 test eax, ebx 确实寄存器间的操作, 会不会导致数据错误???
而 voaltile, 正是约束编译器的, 也就是说它在编译层面保证 mov eax, a test eax, ebx 不会被编译为2条指令, 而时编译为 test *(&a), ebx???
如果发生了这种情况, 两个CPU在相同的时间内执行了不同的赋值语句, 那么 CPU1 和 CPU2谁向谁同步呢???
估计是个随机情况了吧???
单x86下用不用内存屏障都没什么区别。内存屏障针对多CPU下,一个CPU等待另外一个CPU的结果。
Some devices present their control interfaces as collections of memory
locations, but the order in which the control registers are accessed is very
important. For instance, imagine an ethernet card with a set of internal
registers that are accessed through an address port register (A) and a data
port register (D). To read internal register 5, the following code might then
be used:
*A = 5;
x = *D;
but this might show up as either of the following two sequences:
STORE *A = 5, x = LOAD *D
x = LOAD *D, STORE *A = 5
the second of which will almost certainly result in a malfunction, since it set
the address _after_ attempting to read the register.
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |