前几篇文章,我们讨论了如何使用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)的更多相关文章

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

    前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 - 有时候是很多时 ...

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

    前几篇文章,我们讨论了如何使用mutex保护数据及使用使用condition variable在多线程中进行同步.然而,使用mutex将会导致一下问题: 等待互斥锁会消耗宝贵的时间 - 有时候是很多时 ...

  3. 海量并发的无锁编程 (lock free programming)

    最近在做在线架构的实现,在线架构和离线架构近线架构最大的区别是服务质量(SLA,Service Level Agreement,SLA 99.99代表10K的请求最多一次失败或者超时)和延时.而离线架 ...

  4. java轻松实现无锁队列

    1.什么是无锁(Lock-Free)编程 当谈及 Lock-Free 编程时,我们常将其概念与 Mutex(互斥) 或 Lock(锁) 联系在一起,描述要在编程中尽量少使用这些锁结构,降低线程间互相阻 ...

  5. 无锁同步-C++11之Atomic和CAS

    1.概要 本文是无锁同步系列文章的第一篇,主要探讨C++11中的Atomic. 我们知道在C++11中引入了mutex和方便优雅的lock_guard.但是有时候我们想要的是性能更高的无锁实现,下面我 ...

  6. 基于无锁队列和c++11的高性能线程池

    基于无锁队列和c++11的高性能线程池线程使用c++11库和线程池之间的消息通讯使用一个简单的无锁消息队列适用于linux平台,gcc 4.6以上   标签: <无>   代码片段(6)[ ...

  7. C++11原子操作与无锁编程(转)

    不讲语言特性,只从工程角度出发,个人觉得C++标准委员会在C++11中对多线程库的引入是有史以来做得最人道的一件事:今天我将就C++11多线程中的atomic原子操作展开讨论:比较互斥锁,自旋锁(sp ...

  8. 我是如何一步步的在并行编程中将lock锁次数降到最低实现无锁编程

    在并行编程中,经常会遇到多线程间操作共享集合的问题,很多时候大家都很难逃避这个问题做到一种无锁编程状态,你也知道一旦给共享集合套上lock之后,并发和伸缩能力往往会造成很大影响,这篇就来谈谈如何尽可能 ...

  9. Lock Free (无锁并发)

    CAS( compare and swap) 原子操作,保证了如果需要更新的地址没有被其他进程(线程)改动过,那么它可以安全的写入.而这也是我们对于某个数据或者数据结构加锁要保护的内容,保证读写的一致 ...

随机推荐

  1. 读取oracle页面或者进程卡住不动(死锁)

    oracle最坑爹的地方:你insert   update  delete之后  或者kill死锁的时候记得一定要提交事务不然就是死锁卡在那里了 记住  kill死锁也是要提交事务的 select * ...

  2. 华为oj 统计字符串不同字符

    #include <stdio.h> #include <string.h> int firstSingle(char *str) { int hash[128]={0}; f ...

  3. git 笔记记录

    分布式版本控制系统Git 是一套内容寻址文件系统,从核心上来看不过是简单地存储键值对.一: git 本地clone 一个仓库    1. 直接clone一个仓库:        $: git clon ...

  4. C++ list用法

    创建一个list实例并赋值: // 创建实例以及赋值 #include <iostream> #include <list> using namespace std; int ...

  5. Ubuntu下安装搜狗拼音输入法

    Ubuntu默认输入法是ibus输入法,其实用着也可以了,但是说句实话在某些情况下真的不怎么智能.习惯了搜狗所以,查阅资料测试成功后整理如下, 1.安装搜狗拼音 sudo apt-add-reposi ...

  6. CentOS(Linux) - SVN使用笔记(一) - 安装SVN过程及开启和关闭svn服务指令

    1.安装: yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql yum remove subversion 删除旧版 ...

  7. php生成批次唯一code(转)

    /** * @param int $no_of_codes//定义一个int类型的参数 用来确定生成多少个优惠码 * @param array $exclude_codes_array//定义一个ex ...

  8. FileZilla Server下载以及安装使用

    新版本filezilla server已经不能在windows xp和windows20003下使用了 下面是可以在xp和2003下使用的最后版本下载地址 http://pan.baidu.com/s ...

  9. 背包问题lingo求解

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang !背包问题 题目: 8件物品   重量分别为 1,3,4,3,3,1,5,10 价值分别为 2,9 ...

  10. 关于nginx架构探究(2)

    nginx 数据结构 1.Hash table nginx 对虚拟主机的管理使用到了HASH数据结构,假设配置文件里有如下的配置. Server{ listen 192.168.0.1 server_ ...