为什么 Leveldb 中的读操作不需要上锁?

2017-10-30 16:55:00 +08:00
 qianjindapang

下面是 skiplist::insert() 的伪代码:

    node = new Node();
    prev = find_place_to_insert();
    node->next = prev->next;
    memory_barrier();
    prev->next = node;

在对 skiplist 写操作时并没有上锁,当两个线程分别对 skiplist 进行读写时能保证安全吗?我的理解是 Node::next 字段需要是原子变量,而 leveldb 代码中它只是一个普通的 void * 类型。

leveldb 用宏控制的另一个版本中,Node::next 字段确实是stl::atomic类型的,但是没有用 memory barrier。memory barrier 有原子变量的功能?或者说这两者有什么区别?

这个问题针对 reader-writer 竞争,不是 writer-writer 竞争,后者在上层调用中已经解决了。

3003 次点击
所在节点    程序员
3 条回复
qianjindapang
2017-10-30 17:19:11 +08:00
标题写错了,应该是*为什么 Leveldb 中 skiplist 的读操作不需要上锁?*
araraloren
2017-10-31 08:49:20 +08:00
I recommend you learn the c++ memory order and the memory barrier

some arctile:

http://blog.csdn.net/world_hello_100/article/details/50131497
http://blog.csdn.net/cszhouwei/article/details/11730559
qianjindapang
2017-11-02 15:10:16 +08:00
@araraloren 这好像是个比较大的话题,找到了一本书对这块有详细的介绍
<<C++ Concurrency in Action: Practical Multithreading>>

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/401993

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX