Skip to main content
  1. Posts/

Lock-free vs wait-free concurrency

·1069 words·3 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

参考资料

看 leveldb 源码中遇到的,关于 lock-free 和 wait-free..感觉这个讲得不错,我试着翻译一下?

There are two types of non-blocking thread synchronization algorithms - lock-free, and wait-free. Their meaning is often confused. In lock-free systems, while any particular computation may be blocked for some period of time, all CPUs are able to continue performing other computations. To put it differently, while a given thread might be blocked by other threads in a lock-free system, all CPUs can continue doing other useful work without stalls. Lock-free algorithms increase the overall throughput of a system by occassionally increasing the latency of a particular transaction. Most high- end database systems are based on lock-free algorithms, to varying degrees.

有两种无阻塞线程同步算法,一种是 lock-free,一种是 wait-free,它们的含义经常被搞混。在一个 lock-free 系统中,尽管某个特定的计算会被阻塞一段时间,所有 CPU 还是能够继续其他计算。换一种说法,尽管在一个 lock-free 系统中,一个线程可能被其他线程阻塞,所有 CPU 仍然可以继续做其他工作而不停顿。lock-free 算法通过偶尔增加某个特定事务的延迟,提高了系统总体的吞吐率。大多数高端数据库系统都在某种程度上基于 lock-free 算法。

By contrast, wait-free algorithms ensure that in addition to all CPUs continuing to do useful work, no computation can ever be blocked by another computation. Wait-free algorithms have stronger guarantees than lock-free algorithms, and ensure a high thorughput without sacrificing latency of a particular transaction. They’re also much harder to implement, test, and debug. The lockless page cachepatches to the Linux kernel are an example of a wait-free system.

与此相反,wait-free 算法除了保证所有 CPU 继续做其他工作以外,还保证不会有任何计算被另一个计算阻塞。wait-free 算法比 lock-free 算法有更强的保证:在不牺牲某个特定事务延迟的前提下拥有更高的吞吐率。也正因如此,wait-free 算法更加难以实现、测试和调试。Linux 内核的无锁页面缓存(?)就是一个 wait-free 系统的例子。

并发进度保证:阻塞、lock-free 与 wait-free

In a situation where a system handles dozens of concurrent transactions and has soft latency requirements, lock-free systems are a good compromise between development complexity and high concurrency requirements. A database server for a website is a good candidate for a lock-free design. While any given transaction might block, there are always more transactions to process in the meantime, so the CPUs will never stay idle. The challenge is to build a transaction scheduler that maintains a good mean latency, and a well bounded standard deviation.

在一个系统处理几十个并发事务并且对延迟要求不高的情况下,lock-free 系统是开发复杂性和高并发要求之间的良好折衷。一个网站的数据库服务器就是 lock-free 系统很好的例子。尽管某个特定的事务可能被阻塞,但同时总有更多的事务要处理,所以 CPU 永远不会空闲。面临的挑战是构建一个事务调度器,从而获得尽可能低的平均延迟和有界的标准偏差。

In a scenario where a system has roughly as many concurrent transactions as CPU cores, or has hard real-time requirements, the developers need to spend the extra time to build wait-free systems. In these cases blocking a single transaction isn’t acceptable - either because there are no other transactions for the CPUs to handle, minimizing the throughput, or a given transaction needs to complete with a well defined non-probabilistic time period. Nuclear reactor control software is a good candidate for wait-free systems.

在一个并发事务数量与 CPU 内核数量大致相同的场景中,或者在有硬实时要求的场景中,开发人员需要花费额外的时间来构建 wait-free 系统。在这种情况下,阻塞单个事务是不可接受的:要么是因为 CPU 没有其他事务要处理,从而降低了吞吐量;要么是给定的事务需要在一个确定的时间段内完成(相对比较确定的时间的意思?)。核反应堆控制软件就是 wait-free 系统很好的例子。

Related

AWK 初探

·2989 words·6 mins
参考资料: awk_维基百科 awk简明教程 awk是一门比较古老但是很好用的文本处理工具(语言?)

Linux 下各个目录的作用及内容

·2481 words·5 mins
参考:FHS(Filesystem Hierarchy Standard) 其实这东西虽然有一个统一的标准,但是不同发行版,或者同一个发行版的不同版本,差异貌似都蛮大的。所以这里只是理论上各个目录的作用,可能和具体的发行版不符。。