Skip to content

Commit

Permalink
add verifty
Browse files Browse the repository at this point in the history
  • Loading branch information
selfboot committed Oct 10, 2024
1 parent db75e94 commit 26f0fea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"server": "hexo server"
},
"hexo": {
"version": "7.2.0"
"version": "7.3.0"
},
"dependencies": {
"cheerio": "1.0.0",
Expand Down
14 changes: 14 additions & 0 deletions source/_drafts/leveldb-source-memory-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ mathjax: true
---


C++11 引入了std::atomic类型,用于实现无锁编程,原子操作保证在多线程环境下的数据一致性。C++11 定义了6种内存序,用于控制原子操作的同步行为:

- memory_order_relaxed: 最宽松的内存序,只保证当前操作原子性。
- memory_order_consume: 确保后续依赖于当前读取的操作不会被重排到当前读取之前。
- memory_order_acquire: 确保当前读取操作之后的所有读写操作不会被重排到当前操作之前。
- memory_order_release: 确保当前写入操作之前的所有读写操作不会被重排到当前操作之后。
- memory_order_acq_rel: 结合了acquire和release语义。
- memory_order_seq_cst: 最严格的内存序,提供全局的顺序一致性。


这里用了 atomic 原子类型,保证了读写这个值的操作是原子的,不会被其他线程打断。如果不用原子操作,会出现下面这种竞争情况:

Expand All @@ -27,6 +36,11 @@ mathjax: true
下面来分析下这样做会不会有线程同步问题。我们知道,**编译器和处理器可能会对指令进行重排,只要这种重排不违反单个线程的执行逻辑就好**。上面 Insert 操作中,设置新高度和后面的更新节点指针顺序可能会被打乱。导致出现下面的情况:


## 复现指令重排

有什么办法能稳定复现编译器或者 CPU 指令重排导致问题吗?


[Memory Model and Synchronization Primitive - Part 1: Memory Barrier](https://www.alibabacloud.com/blog/597460)

[Memory Model and Synchronization Primitive - Part 2: Memory Model](https://www.alibabacloud.com/blog/memory-model-and-synchronization-primitive---part-2-memory-model_597461)
1 change: 1 addition & 0 deletions source/ads.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google.com, pub-7746897490519544, DIRECT, f08c47fec0942fa0

0 comments on commit 26f0fea

Please sign in to comment.