Notes on memory reorder(CPU level)

现代CPU出于性能考虑采用乱序执行(OOE),可能导致内存重排序。本文聚焦于x86和ARM系列,讨论CPU层面的内存重排序现象及其控制。内存重排序可能导致在多观察者场景下出现意外结果,因此需要通过获取(Acquire)和释放(Release)等内存顺序控制手段来确保正确性。
摘要由CSDN通过智能技术生成

There are memory models in language level like C11 memory model, and CPU level memory model like weakly and strongly ordered memory model. In this article I will talk about memory reorder in CPU in a nutshell and the explicit control over memory access order. I will mainly focus on x86 and ARM series.

x00 What is memory reorder

For performance, nearly all modern CPUs are built with out of order execution(OOE) machenism, whose effect on memory access leads to memory reorder.

So, what does “memory reorder” mean?
Memory access can happen in two ways: Read® and Write(W). Let’s say we have two consecutive instructions in a program to be executed:

[Code1]
	WRITE [addr_0], A			//  write variable A into address addr_0
	WRITE [addr_1], B			//  write variable B into address addr_1 

For the CPU core executing these two instructions, it sees no explicit data dependency between these two instructions (even though that in the programmer’s mind who has a multithreaded and global view of the program, these two instructions may have some subtle dependency). Modern CPU core is allowed to execute such data independent instuctions in any order as long as the final result of the program is correct in its “single-thread” view. Therefore this may result in that effect of WRITE of B is observed before WRITE of A by other observers (any entities, such as another CPU core, device, that interacts with this cpu core can be an observer), and the memory access is “out of order” or “reordered”. This is called memory reorder.
Undoubtably, memory reorder may give unexpected result from programmer’s view. However, in uniprocessor, we rarely encounter unexpected results because while CPU can issue memory access unordered, it will gurantee the data dependency order which is largely eno

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值