一哥们翻译的boost的无锁队列的官方文档
原文地址:http://blog.csdn.net/great3779/article/details/8765103
Boost_1_53_0终于迎来了久违的Boost.Lockfree模块,本着学习的心态,将其翻译如下。(原文地址:http://www.boost.org/doc/libs/1_53_0/doc/html/lockfree.html)
Chapter 17. Boost.Lockfree
第17章.Boost.Lockfree
Table of Contents
目录
简介及动机
例子
解释
数据结构
内存管理
ABA预防
进程间支持
附录
未来发展
Introduction& Motivation
简介和动机
Introduction & Terminology
简介及术语
The term non-blocking denotes concurrent data structures,which do not use traditional synchronization primitives like guards to ensurethread-safety. Maurice Herlihy and Nir Shavit (compare "TheArt of Multiprocessor Programming")distinguish between 3 types of non-blocking data structures, each havingdifferent properties:
术语无阻塞表示并发数据结构,它们不使用传统同步原语例如守卫者来保证线程安全。MauriceHerlihy和NirShavit(相比《多处理器编程艺术》)区分了三种类型的无阻塞数据结构,每种均具有不同的特性:
- data structures are wait-free, if every concurrent operation is guaranteed to be finished in a finite number of steps. It is therefore possible to give worst-case guarantees for the number of operations.
- 无等待数据结构,如果所有并发操作都保证会在有限步骤内完成。因此就有可能给出一个对操作数目的最坏保证。
- data structures are lock-free, if some concurrent operations are guaranteed to be finished in a finite number of steps. While it is in theory possible that some operations never make any progress, it is very unlikely to happen in practical applications.
- 无锁数据结构,如果一些并发操作保证在有限步骤内完成。虽然理论上有些操作可能不会有任何进展,但实际应用中基本不太可能发生。
- data structures are obstruction-free, if a concurrent operation is guaranteed to be finished in a finite number of steps, unless another concurrent operation interferes.
- 无梗阻数据结构,如果除非被另外一个并发操作干预,否则一个并发操作保证在有限步骤内完成。
Some data structures can only be implemented in a lock-freemanner, if they are used under certain restrictions. The relevant aspects forthe implementation of boost.lockfree
are thenumber of producer and consumer threads. Single-producer (sp) or multiple producer (mp) means that only a single thread ormultiple concurrent threads are allowed to add data to a data structure. Single-consumer (sc) or Multiple-consumer (mc) denote the equivalent for the removalof data from the data structure.
如果使用在一定的限制条件下,一些数据结构只能被无锁的方式实现。与boost.lockfree实现相对应的是生产者线程和消费者线程的数目。单生产者(sp)或多生产者(mp)意味着只有一个线程或多个并发线程被允许添加数据至某数据结构中。单消费者(sc)或多消费者(mc)则对应于从数据结构中移除数据。
Properties of Non-Blocking Data Structures
无阻塞数据结构的性质
Non-blocking data structures do not rely on locks and mutexes toensure thread-safety. The synchronization is done completely in user-spacewithout any direct interaction with the operating system [4].This implies that they are not prone to issues like priority inversion (alow-priority thread needs to wait for a high-priority thread).
无阻塞数据结构不依赖于锁和互斥量来保证线程安全。同步完全在用户空间中完成,而不需要与操作系统的任何直接交互。这意味着它们不容易出现例如优先反转(低优先级线程需要等待高优先级线程)等问题。
Instead of relying on guards, non-blocking data structures require atomicoperations (specificCPU instructions executed without interruption). This means that any threadeither sees the state before or after the operation, but no intermediate statecan be observed. Not all hardware supports the same set of atomic instructions.If it is not available in hardware, it can be emulated in software usingguards. However this has the obvious drawback of losing the lock-free property.
无阻塞数据结构需要原子操作(特定的CPU执行指令不中断)而不是依赖于“卫兵”。这意味着任何线程只能看到操 作之前或之后的状态,而不能观察到任何中间状态。不是所有的硬件都支持同样的原子指令集。如果硬件上不支持,则可以在软件上使用“卫兵”来模拟。但这因为 失去了无锁的性质而具有明显的缺陷,。
Performance of Non-Blocking Data Structures
无锁数据结构的性能
When discussing the performance of non-blocking data structures,one has to distinguish between amortized and worst-case costs. The definition of 'lock-free'and 'wait-free' only mention the upper bound of an operation. Thereforelock-free data structures are not necessarily the best choice for every usecase. In order to maximise the throughput of an application one should conside