码迷,mamicode.com
首页 > 其他好文 > 详细

无锁编程

时间:2015-05-06 22:32:37      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

 1 #ifndef MUTEX_CLASS_H
 2 #define MUTEX_CLASS_H
 3 
 4 class mutex_class
 5 {
 6 public:
 7     mutex_class();
 8     void lock();
 9     void unlock();
10 
11 private:
12     //以下为使用,和一般的锁差不多
13     int MUTEX_VALUE;
14 };
15 
16 #endif // MUTEX_CLASS_H
 1 #include "mutex_class.h"
 2 #include <sched.h>
 3 
 4 #define LOCK_VALUE  0
 5 #define   UNLOCK_VALUE 1
 6 //nt MUTEX_VALUE  = LOCK_VALUE;
 7 #define FREE_LOCK(MUTEX_VALUE_LOCK) 8     while (!(__sync_bool_compare_and_swap ( 9               &MUTEX_VALUE_LOCK,LOCK_VALUE, 10               UNLOCK_VALUE) )) 11                  sched_yield();
12 
13  #define FREE_UNLOCK(MUTEX_VALUE_LOCK)14          __sync_bool_compare_and_swap (&MUTEX_VALUE_LOCK, UNLOCK_VALUE, LOCK_VALUE);
15 
16 
17 
18 
19 mutex_class::mutex_class()
20 {
21     MUTEX_VALUE = LOCK_VALUE;
22 }
23 
24 void mutex_class::lock()
25 {
26     FREE_LOCK(MUTEX_VALUE)
27 }
28 
29 void mutex_class::unlock()
30 {
31     FREE_UNLOCK(MUTEX_VALUE)
32 }

无锁编程

标签:

原文地址:http://www.cnblogs.com/sangzaohaishui/p/4483313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!