cc2530 iar debug release 运行结果不一致_内存一致性模型Memory Consistency Model

10ebbf3ef4038669a49b7af1be079f9e.png

Ordering: Why and How?

08389f3000d8d0cf0e580d6a4e515256.png

Our intuitions: Each thread proceeds in program order. Reading a location should return the latest value written (by any thread).

what does "latest" mean exactly? (1)within a thread, it can be defined by program order; (2)across threads, unpredictable without consistency, need Memory Consistency Model.

dc7280f2837505e4e5769faa3b1b2eaa.png

Single thread execution model (C++03): Program will behave as-if it was you. We can not observe optimizations.

43944d8af4175e2c56aa5310f3623e32.png

优化会加cache与buffer,导致内存一致性问题。

d8ee845cdf0886b96ee79c9a78db8c34.png

优化加入"out of order" pipelining与conditional branches, memory accesses may be performed out-of-order。

e58fe4d6e9bdc37242835175b8a1fe01.png

多核会造成不安全。

1b2d764d5297792866c80f99daeba79c.png

cache传播的可见性。

456fc30ceb46eed6e3eb7dc223642855.png

What abot two threads? Optimizations become observable. Memory accesses interleaved.

9e84912ebbf24d16e0286a733f37ed91.png

The CPU is within its rights to reorder the statements within both Thread P0() and Thread P1(), even on relatively strongly ordered systems such as x86.

2a497f01612c294d41340475a8faa7b7.png

When a given thread observes memory accesses from a different thread: those memory accesses can be (almost) arbitrarily jumbled around. 重排方式:

3f98cb899dffa16a8978f59f6eebaa9e.png

(1)不同地址访问操作的排列组合。

fca96bbe29a83f1e66638bf8aeca7360.png

(2)基址+偏移量(数组形式)也可以重排。

b79a90aa56f8d4c97801b27bb12839e9.png

(3)设备地址操作也可以重排。

Base Guarantee

198e8335e493b80976707ac64ae31d32.png

Optimizations may break "naive" concurrent algorithms.

85d47585c51e39b7876be71a4f6055c4.png

单CPU访问不同地址怎样优化结果都最终一致

53a10dee3d1adfb4ae39631bef484ea0.png

单CPU访问相同地址怎样优化结果都最终一致

d8e8fa2225aae57c9ca280fd84ef5f4f.png

什么时候使用内存屏障?

4405702f3ab8b49d78af879811743d19.png

Memory Order Semantics

20830a6a26edbddcfe67fcecf96e0f8f.png

直觉模型是顺序一致性。

3a4f3c7e84a28c46bd2919343c9edfab.png

the result of any execution is the same as-if:(1)the operations of all threads are executed in some sequential order;(2)the operations of each thread appear in this sequence in the order specified by their program.

997ec2830801fa4d1e78728321fefbb9.png

需要硬件支持。

54eb455f15a754a59da6ee5f7b2c1fd0.png

保证全局可见性。

de0fd7e602d62a0e1663fd2f6d00094b.png

Processor issues accesses one-at-a-Lme and stalls for completion. Low processor utilization (17% - 42%) even with caching.

5a701e5345636584b8bf29c5696e9815.png

TSO模型。

84e909f1952484d8dda05a8f9f3e03b9.png

program oder在多线程执行时是分布式的,memory order是全局的。

211fb7822f51dfbff0bbc55e0e71198b.png

happens-before语义。

b8b6bc166f8f73a6f0c2162c2a883fd6.png

Inter-thread happens before需要synchronize同步控制。

2ce474e8f8176eeeb56c5cad4e8a416b.png

Synchronize (the easy way)

be6242c23ff4f33e6e37760d6324e358.png

Synchronization来避免Data Races。

97b4b5e8d5f697bd9b2c56c27076c05c.png

使用synchronize操作保证happens before。

110c8986ef3433cf94070803608f1b45.png

Mutual exclusive execution of critical code blocks. lock() on the same mutex object.

7c7b6791d527a689e70f0b43f1e8fe8b.png

std::atomic<>模板特化,对整型、指针类型、浮点型提供原子运算。

6fb288f7ba03e386d198f0e1768c88a9.png

同时使用CAS原子操作可以方便实现lock-free数据结构。

acbcda5e9c226951c853963a79cc5a1e.png

C++ Memory Model (Programing Level)

d2326a06097940933a81f78b2d8f84d9.png

内存一致性模型,c++11有对应。

(1) Relaxed Memory Order

0113f438a64f039b2630890fa138b2a3.png

只保证原子性。

9527da1d0ace1b0213db85b9b4c4e31d.png

Memory operations performed by the same thread on the same memory location are not reordered with respect to the modification order.

df83f1cf18ef23f1a12a9a5abd72b4e2.png

Each memory location has a total modification order (however, this order cannot be observed directly).

(2) Release Memory Order

aa2a044e1e097be7e8d2c212722a9128.png

用于store操作。

92ff2830a81c29490a6ca87d713a4939.png

Not same as write barrier.

5d80415706cf1d10a33cde576e3ef5b4.png

单独使用没有意义。

(3) Acquire Memory Order

68ca150043c5457745677b998efce0b6.png

用于读操作。

3759f2405655d69ff11e528aff21eb5e.png

Not same as read barrier.

7efd6ac2659cc6384f0c45b10374cfdf.png

单独使用没有意义。

(4) Release-Acquire Model

07fe47af68ce621ae998d56a2db93674.png

Release-Acquire 配对使用。

2f41084eb7db3387c481f9588ddf201c.png

一个线程使用release,另一个线程使用require。

6b8bba81e40feece6b78fcd20145f5a3.png

用于RMW。

6b1414cbb9beaa69e708316c53e9d196.png

通过相同变量的release与acquire操作来同步。

bcddabc45c6a7f0aff018cdc51619a0e.png

注意使用while循环等待需要的条件,否则只是运行时某一种状态。

0e20b8572eb75c5cadacd16e251fd58a.png

拥有传递性。

59934e7b3b31bbb769390ad8e8f6f281.png

(5) Consume Memory Order

08cf509b75c4c7df46545df1d3aa0842.png

特定条件下的acquire model。

38823d1680404e2e41b87d5c692c922a.png

后续操作依赖于Consume的变量。

c93c43602d2f18404c49b716b40a24ae.png

Release-Consume配合使用。

7d8cafe5622536dcbf692e777b67f001.png

指针解引用与数组索引操作都属于依赖关系。

(6) Sequentially Consistent Memory Order

77e04122be34aae24d3531f11def53bc.png

强一致模型。

92d889355d110ced078ddd4afd32eb29.png

没有SC模型则dekker算法失败。f1、f2读取交叉,都读的旧值。需要将store、load都使用memory_order_seq_cst。

dae6ba30506c0e7323e37dd84aa1c4fe.png

由于缓存存在,读到旧值。不同CPU看到ab的10与01两种序列。

c41c6fc950981c83e0896c2ca4ecbf14.png

有了强一致模型,不同CPU只会看到ab值的一种序列。

f288201d0cb7bdaa7e6acb885934fc4b.png

没有强一致性模型,则(0,0)产生。

978550ddfe1ffc6d48464c23f98bbc37.png

S1、S2交叉。

9bae98e8e38d7e6e8535318403cf4e58.png

一般计数器只需要原子操作,不需要同步顺序性。

(7) Atomic Thread Fence

95acd6b1ee26ac872758e5ec492c3569.png

thread fence与变量的atomic操作搭配使用。generally slower than memory barriers associated with an atomic operation。

1bccc1db3f65f48bf5e7d11988d10dc1.png

fence使用release,store(a)只需要relaxed,相当于store(a, release)。

421d5dedda8b6670d32985ebd01200fb.png

fence使用acquire,store(a)只需要relaxed,相当于load(a, acquire)。

d8a9a5242dd20b30a8a6f017123e57d2.png

fence使用release-acquire搭配。

References

  1. https://en.cppreference.com/w/cpp/atomic/memory_order
  2. https://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/
  3. https://www.kernel.org/doc/Documentation/memory-barriers.txt
  4. Memory barriers in C
  5. C++ Memory Model, Valentin Ziegler
  6. Memory Consistency, CMU 15-418/15-618, Fall 2016
  7. 高并发编程--多处理器编程中的一致性问题 GTHub:高并发编程--多处理器编程中的一致性问题(上)
  8. volatile与内存屏障总结 郑传军:volatile与内存屏障总结
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值