前几篇文章,我们讨论了如何使用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 { 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
intoexpected
(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
andfailure
respectively. In the (2) and (4) versionsorder
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.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
陈皓同学的精彩博文: http://coolshell.cn/articles/8239.html