天天看点

并发编程(三): 使用C++11实现无锁stack(lock-free stack)

前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步。然而,使用mutex将会导致一下问题:

等待互斥锁会消耗宝贵的时间 — 有时候是很多时间。这种延迟会损害系统的scalability。尤其是在现在可用的core越多越多的情况下。

低优先级的线程可以获得互斥锁,因此阻碍需要同一互斥锁的高优先级线程。这个问题称为优先级倒置(priority inversion )。

可能因为分配的时间片结束,持有互斥锁的线程被取消调度。这对于等待同一互斥锁的其他线程有不利影响,因为等待时间现在会更长。这个问题称为锁护送(lock convoying)。

互斥锁的问题还不只这些。早在1994年10月,john d. valois 在拉斯维加斯的并行和分布系统系统国际大会上的一篇论文—《implementing lock-free queues》已经研究了无锁队列的实现,有兴趣的可以拜读一下。

实现无锁数据结构的基础是cas:compare & set,或是 compare & swap。cas用c语言描述的代码(来自wikipedia compare and swap)

cas是个原子操作,保证了如果需要更新的地址没有被他人改动多,那么它可以安全的写入。而这也是我们对于某个数据或者数据结构加锁要保护的内容,保证读写的一致性,不出现dirty data。现在几乎所有的cpu指令都支持cas的原子操作,x86下对应的是 cmpxchg 汇编指令。现在,我们将使用cas来实现无锁的stack,然后你就能够理解cas的用法了。

c++11中cas实现:

please refer to http://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange to more information.

对上面的版本进行一下说明。翻译自上述url:

atomically compares the value stored in <code>*this</code> with the value of <code>expected</code>, and if those are equal, replaces the former with <code>desired</code> (performs read-modify-write operation). otherwise, loads the actual value stored in<code>*this</code> into<code>expected</code> (performs load operation).

自动的比较*this的值和expect的值,如果相等,那么将*this的值替换为desired的值(进行读-修改-写操作)。否则如果不相等,那么将*this的值存到expected处。

伪码就是:

the memory models for the read-modify-write and load operations are<code>success</code> and<code>failure</code> respectively. in the (2) and (4) versions<code>order</code> is used for both read-modify-write and load operations, except thatstd::memory_order_release andstd::memory_order_relaxed are used for the load operation iforder==std::memory_order_acq_rel, ororder==std::memory_order_release respectively.

<code>success</code>对应于read-modify-write的内存模型;failure则对应于失败时的load。对于order = std::memory_order_seq_cst的函数,那么该memory order适用于read-modify-write and load,除非是如果order==std::memory_order_acq_rel,那么load将使用std::memory_order_release;如果order==std::memory_order_release,那么load将使用std::memory_order_relaxed。

更多信息memory order请阅读:http://en.cppreference.com/w/cpp/atomic/memory_order

the weak forms (1-2) of the functions are allowed to fail spuriously, that is, act as if*this!= expected even if they are equal. when a compare-and-exchange is in a loop, the weak version will yield better performance on some platforms. when a weak compare-and-exchange would require a loop and a strong one would not, the strong one is preferable.

weak形式允许假失败,该函数直接比较原子对象所封装的值与参数 expected 的物理内容,所以某些情况下,对象的比较操作在使用 operator==() 判断时相等,但 compare_exchange_weak 判断时却可能失败,因为对象底层的物理内容中可能存在位对齐或其他逻辑表示相同但是物理表示不同的值(比如 true 和 2 或 3,它们在逻辑上都表示"真",但在物理上两者的表示并不相同)。可以虚假的返回false(和expected相同)。若本atomic的t值和expected相同则用val值替换本atomic的t值,返回true;若不同则用本atomic的t值替换expected,返回false。  

与compare_exchange_weak 不同, strong版本的 compare-and-exchange 操作不允许(spuriously 地)返回 false,即原子对象所封装的值与参数 expected 的物理内容相同,比较操作一定会为 true。不过在某些平台下,如果算法本身需要循环操作来做检查, compare_exchange_weak 的性能会更好。因此对于某些不需要采用循环操作的算法而言, 通常采用compare_exchange_strong 更好

下面代码部分来自http://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange。

注意在这里添加了stack的构造函数,把head初始化为nullptr。如果不初始化它为nullptr,那么使用链表存储的stack将无法确定终点在哪儿。。。

首先看一下push的实现:

主要是理解这两句:

可以简单用一下代码来概括该调用的效果:

因此,如果没有其他的线程push,那么head将指向当前的new_node,push完成。否则,说明其他线程push过新数据,那么将当前push的新节点重新放到顶端,此时的head是最新的head。这样,通过cas,我们可以实现了thread-safe stack。

接下来看一下pop:

我们为什么要限制result != nullptr?因为有可能当前stack仅有一个元素,线程b在pop时被调度,线程a pop成功,那么线程b再pop就会出问题。

其实,上述的pop可以简化,因为result其实在failed时候已经更新为head了。因此简化代码可以是:

尊重原创,转载请注明出处: anzhsoft http://blog.csdn.net/anzhsoft/article/details/19125619

参考资料:

1. http://en.wikipedia.org/wiki/compare-and-swap

2. http://en.wikipedia.org/wiki/fetch-and-add

3. http://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange

4. http://technet.microsoft.com/zh-cn/hh874698

更多学习:

1. gcc实现 http://www.oschina.net/translate/a-fast-lock-free-queue-for-cpp?cmp

2. gcc实现 http://www.ibm.com/developerworks/cn/aix/library/au-multithreaded_structures2/index.html

陈皓同学的精彩博文: http://coolshell.cn/articles/8239.html

继续阅读