关于lock-free的问题,经典模型就是mpmc 无锁队列:具体可以参考之前的文章:
- lock-free队列
- lock-free-progress-guarantees
- 无锁数据结构库liblfds
- dpdk lock-free-mpmc :https://dpdk-docs.readthedocs.io/en/latest/prog_guide/
- 老毛子的一篇文章:https://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
- cmu课件:https://www.cs.cmu.edu/~410-s05/lectures/L31_LockFree.pdf
lock-free的意思就是:只要有task在往前走(make progress),那总有task能往前走;
wait-free的意思是,只要某个task活着,不管别人在干什么,它都一定能往前走。
为什么有lock就不是lock-free了,因为只要拿到lock的那个task挂掉不动了,这个系统就完蛋了,每个task都不能再往前走了。
Lock-free和wait-free的区别在哪,在于starvation。大部分的lock-free的算法都是基于某些竞争性的原语的(primitives)。Starvation一般都是在有多个task竞争(race)的时候,也许有一个运气好的task总是在赢,那么别的task就很可怜地什么都做不了了。这不是因为没轮到他们,而只是因为他们运气坏。实际上整个系统还是在运行的,就是那个运气好的task自己一直在跑,所以这不是死锁(dead-lock)。有人把这个东西定义为live-lock---(翻译成活锁吧)。
很少有天然的wait-free的算法(最天然的那个,single-reader-single-writer queue大家都知道了)。新的wait-free的算法,大都是靠胜者的施舍,也就是说有task一直赢的话,就分给输家一点,让输家能往前动一下。
wait-free的论文:A Wait-free Queue as Fast as Fetch-and-Add 以及: Wait-Free Queues With Multiple Enqueuers and Dequeuers ∗
lock-free相关质料:
- Linked List
lock free linked lists using compare-and-swap, John D. Valois
A Pragmatic Implementation of Non-Blocking Linked-Lists, Timothy L. Harris
- Queue
A Non-Blocking Concurrent Queue Algorithm, Bruno Didot
Implementing Lock-Free Queues, John D. Valois
SnapQueue: Lock-Free Queue with Constant Time Snapshots, Aleksandar Prokopec
- Tree
A Practical Concurrent Binary Search Tree, Nathan G. Bronson, etc.
Non-blocking k-ary Search Trees, Trevor Brown and Joanna Helga
Non-blocking Binary Search Trees, Faith Ellen, etc.
Range Queries in Non-blocking k-ary Search Trees, Trevor Brown
Chromatic binary search trees A structure for concurrent rebalancing, Otto Nurmi, etc.
- Trie
Concurrent Tries with Efficient Non-Blocking Snapshots, Aleksandar Prokopec
书籍
The Art of Multiprocessor Programming, Maurice Herlihy, Nir Shavit
C++ Concurrency in Action: Practical Multithreading, Anthony Williams
Is Parallel Programming Hard, And, If So, What Can You Do About It?, Paul E. McKenney, etc
Memory Ordering in Modern Microprocessors, Paul E. McKenney
Practical lock-freedom, Keir Fraser
博客
An Introduction to Lock-Free Programming
Trevor Brown's home page
Lock-free Data Structures. 1
何登成的技术博客
Concurrency Freaks
Paul E. McKenney
Paul E. McKenney: Read-Copy Update (RCU)
Library and Some Implementation
khizmax/libcds · GitHub
concurrencykit/ck · GitHub
gramoli/synchrobench · GitHub (README.md中有很多论文)
scala/src/library/scala/collection/concurrent at 2.11.x · scala/scala · GitHub (Concurrent Tries with Efficient Non-Blocking Snapshots的实现)
Talk and Presentation
CPU Cache and Memory Ordering——并发程序设计入门
CppCon 2014: Herb Sutter "Lock-Free Programming (or, Juggling Razor Blades), Part I"
CppCon 2014: Jeff Preshing "How Ubisoft Develops Games for Multicore - Before and After C++11"
CppCon 2015: Michael Wong “C++11/14/17 atomics and memory model..."
John D. Valois
Nir Shavit
Paul E. McKenney
Trevor Brown
Aleksandar Prokopec
Herb Sutter
Doug lea
Feifei Li发了篇FAST 2020,lock-free地让KV store的热点数据更快地被访问,从而提高整个KV store的performance。
HotRing: A Hotspot-Aware In-Memory Key-Value Store,
by J. Chen, L. Chen, S. Wang, G. Zhu, Y. Sun, H. Liu, F. Li, In Proceedings of 18th USENIX Conference on File and Storage Technologies (USENIX FAST 2020), pages 239-252, Santa Clara, USA, February 2020.
数组 (vector)
Damian Dechev, Peter Pirkelbauer, Bjarne Stroustrup, Lock-free Dynamically Resizable Arrays
Steven Feldman, Carlos Valeraleon, Damian Dechev, An Efficient Wait-Free Vector
哈希表 (hash table)
Shalev, Ori, and Nir Shavit, Split-ordered lists: Lock-free extensible hash tables
Michael, Maged M, High performance dynamic lock-free hash tables and list-based sets
队列 (queue)
Michael, Maged M., and Michael L. Scott, Simple, fast, and practical non-blocking and blocking concurrent queue algorithms
跳跃列表 (skip-list)
Herlihy, Maurice, et al., A simple optimistic skiplist algorithm
Fraser, Keir, and Tim Harris, Concurrent programming without locks 注:Fraser的实现被认为是效率最高,代码可以从作者网站下载。
优先队列 (priority queue)
Lindén, Jonatan, and Bengt Jonsson, A skiplist-based concurrent priority queue with minimal memory contention