文章目录
- 三种内存模型
- 四种读写关系
- Sequential Consistency
- Acquire-Release 模式
- Release-Consume 模式
- Relaxed模式
三种内存模型
这篇文章讲的很好
https://www.codedump.info/post/20191214-cxx11-memory-model-2/
三种内存模型(https://www.codedump.info/post/20191214-cxx11-memory-model-1/):
- Sequential Consistency 顺序一致性,简称SC
- Total Store Ordering, 全存储排序,简称TSO
- Relaxed memory models,松弛型内存模型
四种读写关系
Sequential Consistency
memory_order_seq_cst
,即顺序一致性模型。
Acquire-Release 模式
memory_order_release
前面不会被reord到本句之后;memory_order_acquire
之后的代码不会被reorder到本句之前;memory_order_acq_rel
同时包含acquire和release标志。
这是一段实践代码,代码简单明确:https://www.cnblogs.com/lizhanzhe/p/10893016.html
#include <thread>
#include <chrono>
#include <mutex>
#include <thread>
#include <assert.h>
#include <atomic>std::atomic<int> a=0, b=0, c = 0;void t1_fun() {a = 1;b.store(2, std::memory_order_relaxed); //relaxed,松散的//memory_order_release,类似于mutex的unlock,自身线程中它之前的读写语句都会执行完,不会被优化到本句之后c.store(3, std::memory_order_release);}
void t2_fun() {//memory_order_acquire, 类似于mutex的lock,自身线程它后面的读写语句一定是在后面执行的,不会被优化到本句之前while (c.load(std::memory_order_acquire) != 3); // 以下 assert 永远不会失败 assert(a == 1 && b == 2);assert(b.load(std::memory_order_relaxed) == 2);
}
int main() {std::thread t1(t1_fun);std::thread t2(t2_fun);t1.join();t2.join();
}
Release-Consume 模式
memory_order_consume
,只约束mutex对象的memory order,不约束上下文中其他变量
a = 0;
c = 0;
thread 1:{a = 1; c.store(3, memory_order_release);
}
thread 2:{ //consume只约束mutex对象的memory order,不约束上下文中其他变量while (c.load(memory_order_consume) != 3) ; assert(a == 1); // assert 可能失败也可能不失败
}
Relaxed模式
松散的,不约束。线程内部可以reorder。