使用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)
int compare_and_swap (int* reg, int oldval, int newval)
{
ATOMIC();
int old_reg_val = *reg;
if (old_reg_val == oldval)
*reg = newval;
END_ATOMIC();
return old_reg_val;
}
CAS是个原子操作,保证了如果需要更新的地址没有被他人改动多,那么它可以安全的写入。而这也是我们对于某个数据或者数据结构加锁要保护的内容,保证读写的一致性,不出现dirty data。现在几乎所有的CPU指令都支持CAS的原子操作,X86下对应的是 CMPXCHG 汇编指令 。 现在,我们将使用CAS来实现无锁的stack,然后你就能够理解CAS的用法了。
C++11中CAS实现:
template< class T>
struct atomic<T*>
{
public:
bool compare_exchange_weak( T& expected, T desired,
std::memory_order success,
std::memory_order failure );
bool compare_exchange_weak( T& expected, T desired,
std::memory_order success,
std::memory_order failure ) volatile;
bool compare_exchange_weak( T& expected, T desired,
std::memory_order order =
std::memory_order_seq_cst );
bool compare_exchange_weak( T& expected, T desired,
std::memory_order order =
std::memory_order_seq_cst ) volatile;
bool compare_exchange_strong( T& expected, T desired,
std::memory_order success,
std::memory_order failure );
bool compare_exchange_strong( T& expected, T desired,
std::memory_order success,
std::memory_order failure ) volatile;
bool compare_exchange_strong( T& expected, T desired,
std::memory_order order =
std::memory_order_seq_cst );
bool compare_exchange_strong( T& expected, T desired,
std::memory_order order =
std::memory_order_seq_cst ) volatile;
...
};
Please refer to http://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange to more information.
对上面的版本进行一下说明。翻译自上述url:
Atomically compares the value stored in *this with the value of expected , and if those are equal, replaces the former with desired (performs read-modify-write operation). Otherwise, loads the actual value stored in *this into expected (performs load operation).
自动的比较*this的值和expect的值,如果相等,那么将*this的值替换为desired的值(进行读-修改-写操作)。否则如果不相等,那么将*this的值存到expected处。
伪码就是:
if *this == expected:
*this = desired;
else:
expected = *this;
The memory models for the read-modify-write and load operations are success and failure respectively. In the (2) and (4) versions order is used for both read-modify-write and load operations, except that std::memory_order_release and std::memory_order_relaxed are used for the load operation if order == std:: memory_order_acq_rel, or order == std:: memory_order_releaserespectively.
success 对应于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。
#include <atomic>
#include <string>
#include <iostream>
using namespace std;
template<typename T>
struct node
{
T data;
node* next;
node(const T& data) : data(data), next(nullptr) {}
}; template<typename T>
class stack
{
std::atomic<node<T>*> head; public:
stack():head(nullptr){}
void push(const T& data);
T pop();
};
注意在这里添加了stack的构造函数,把head初始化为nullptr。如果不初始化它为nullptr,那么使用链表存储的stack将无法确定终点在哪儿。。。
首先看一下push的实现:
void push(const T& data)
{
node<T>* new_node = new node<T>(data); // put the current value of head into new_node->next
new_node->next = head.load(std::memory_order_relaxed); // now make new_node the new head, but if the head
// is no longer what's stored in new_node->next
// (some other thread must have inserted a node just now)
// then put that new head into new_node->next and try again
while(!head.compare_exchange_weak(new_node->next,
new_node,
std::memory_order_release,
std::memory_order_relaxed))
; // the body of the loop is empty
}
主要是理解这两句:
head.compare_exchange_weak(new_node->next,
new_node,
可以简单用一下代码来概括该调用的效果:
if ( head == new_node->new){
head = new_node;
return true;
}
else{
new_node->next = head;
return false;
}
因此,如果没有其他的线程push,那么head将指向当前的new_node,push完成。否则,说明其他线程push过新数据,那么将当前push的新节点重新放到顶端,此时的head是最新的head。这样,通过CAS,我们可以实现了thread-safe stack。
接下来看一下pop:
T pop()
{
while(1){
auto result = head.load(std::memory_order_relaxed);
if (result == nullptr)
throw std::string("Cannot pop from empty stack");
if(head.compare_exchange_weak(result,result->next,
std::memory_order_release,
std::memory_order_relaxed))
return result->data;
}
}
我们为什么要限制result != nullptr?因为有可能当前stack仅有一个元素,线程B在pop时被调度,线程A pop成功,那么线程B再pop就会出问题。
其实,上述的pop可以简化,因为result其实在failed时候已经更新为head了。因此简化代码可以是:
T pop()
{
auto result = head.load(std::memory_order_relaxed);
while( result != nullptr && !head.compare_exchange_weak(result,result->next,
std::memory_order_release,
std::memory_order_relaxed));
if( result != nullptr)
return result->data;
else
throw std::string("Cannot pop from empty stack");
}
尊重原创,转载请注明出处: 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
使用C++11实现无锁stack(lock-free stack)的更多相关文章
- 并发编程(三): 使用C++11实现无锁stack(lock-free stack)
前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 - 有时候是很多时 ...
- 并发编程入门(三): 使用C++11实现无锁stack(lock-free stack)
前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 - 有时候是很多时 ...
- 海量并发的无锁编程 (lock free programming)
最近在做在线架构的实现,在线架构和离线架构近线架构最大的区别是服务质量(SLA,Service Level Agreement,SLA 99.99代表10K的请求最多一次失败或者超时)和延时.而离线架 ...
- java轻松实现无锁队列
1.什么是无锁(Lock-Free)编程 当谈及 Lock-Free 编程时,我们常将其概念与 Mutex(互斥) 或 Lock(锁) 联系在一起,描述要在编程中尽量少使用这些锁结构,降低线程间互相阻 ...
- 无锁同步-C++11之Atomic和CAS
1.概要 本文是无锁同步系列文章的第一篇,主要探讨C++11中的Atomic. 我们知道在C++11中引入了mutex和方便优雅的lock_guard.但是有时候我们想要的是性能更高的无锁实现,下面我 ...
- 基于无锁队列和c++11的高性能线程池
基于无锁队列和c++11的高性能线程池线程使用c++11库和线程池之间的消息通讯使用一个简单的无锁消息队列适用于linux平台,gcc 4.6以上 标签: <无> 代码片段(6)[ ...
- C++11原子操作与无锁编程(转)
不讲语言特性,只从工程角度出发,个人觉得C++标准委员会在C++11中对多线程库的引入是有史以来做得最人道的一件事:今天我将就C++11多线程中的atomic原子操作展开讨论:比较互斥锁,自旋锁(sp ...
- 我是如何一步步的在并行编程中将lock锁次数降到最低实现无锁编程
在并行编程中,经常会遇到多线程间操作共享集合的问题,很多时候大家都很难逃避这个问题做到一种无锁编程状态,你也知道一旦给共享集合套上lock之后,并发和伸缩能力往往会造成很大影响,这篇就来谈谈如何尽可能 ...
- Lock Free (无锁并发)
CAS( compare and swap) 原子操作,保证了如果需要更新的地址没有被其他进程(线程)改动过,那么它可以安全的写入.而这也是我们对于某个数据或者数据结构加锁要保护的内容,保证读写的一致 ...
随机推荐
- EasyInvoice 简介
注:本文首发于博客园 EasyInvoice 简介,转载请保留本链接 EasyInvoice(简称 EI) 是一款专门为网上卖家量身打造的管理进销存的软件. 1. 简介 解决卖家日常经营中一直存在的商 ...
- Entrez检索实例 - NCBI
题目:已知来豆荚斑驳病毒(bean pod mottle virus,BPMV)的名字,查询BPMV基因组信息.核酸序列信息.蛋白序列信息和结构信息 解答: 1.直接搜索,点genome,即可看到病毒 ...
- Java 图片切圆角,消除锯齿
public static BufferedImage setBorderRadius(BufferedImage srcImage, int radius){ int width = srcImag ...
- 从html字符串中获取div内容---jquery
思考的问题: 怎么在一个网页的div中嵌套另外的网页(不使用inclue,iframe和frame,不使用他们的原因,include只能嵌套静态网页,iframe对网络爬虫影响,frame嵌套网页无法 ...
- js文本框失去焦点事件与按钮单击事件的冲突问题
<input type="text" id="txt" name="txt"/> <input type="bu ...
- js获取下拉列表(select)选中项的值和文本
获取下拉列表选中项的值和文本(select) <html> <head> <meta charset="utf-8"/> <title&g ...
- php对象中类的继承性访问类型控制
类型的访问控制通过使用修饰符允许开发人员对类中成员的访问进行限制.这是PHP5的新特性,也是OOP语言中的重要特性,大多数OOP语言都已支持此特性.PHP5支持如下三种访问修饰符,在类的封装中我们已经 ...
- python学习第六天 条件判断和循环
总归来讲,学过C语言的同学,对条件判断和循环并不陌生.这次随笔只是普及一下python的条件判断和循环对应的语法而已. 条件判断: 不多说,直接贴代码: age = 23 if age >= 6 ...
- Codeforces 540D Bad Luck Island
http://codeforces.com/problemset/problem/540/D 题目大意: 会出石头.剪刀.布的人分别有r,s,p个,他们相互碰到的概率相同,输的人死掉,问最终活下去的人 ...
- poj3164 (朱刘算法 最小树形图)
题目大意:给定n个点坐标,m条有向边,要求最小树形图. 题解:直接上模板,前面打的 vis[v]=i一直把i打成1,一直TLE. #include<iostream> #include&l ...