关于内存屏障
Ldd上说内存屏障的编译效果是
把当前CPU寄存器中所有修改的数值保存到内存中,需要这些数据的时候再重新读出来。
我看到了某些代码上只有一句:
#define barrier() __asm__ __volatile__("": : :"memory")
查了一下GCC的说明:
If your assembler instructions access memory in an unpredictable fashion, add ‘memory’
to the list of clobbered registers. This will cause GCC to not keep memory values cached in
registers across the assembler instruction and not optimize stores or loads to that memory.
You will also want to add the volatile keyword if the memory affected is not listed in the
inputs or outputs of the asm, as the ‘memory’ clobber does not count as a side-effect of the
asm. If you know how large the accessed memory is, you can add it as input or output but
if this is not known, you should add ‘memory’.
似乎只完成了后半部分的功能,就是说不把数据cache在寄存器里面,用到的时候再去读。这里好像隐喻了在寄存器中的值会被写到内存中,不然以后的读取还是会出问题的,但不是很确定。
大家有没有什么资料可以证实GCC是怎么做的?