首先是添加注释的源码:
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
#ifndef MUDUO_BASE_ATOMIC_H
#define MUDUO_BASE_ATOMIC_H
#include "muduo/base/noncopyable.h"
#include <stdint.h>
namespace muduo
{
namespace detail
{
template<typename T>
class AtomicIntegerT : noncopyable// 不可拷贝
{
public:
AtomicIntegerT()
: value_(0)
{
}
// uncomment if you need copying and assignment
//
// AtomicIntegerT(const AtomicIntegerT& that)
// : value_(that.get())
// {}
//
// AtomicIntegerT& operator=(const AtomicIntegerT& that)
// {
// getAndSet(that.get());
// return *this;
// }
T get()
{
// in gcc >= 4.7: __atomic_load_n(&value_, __ATOMIC_SEQ_CST)
return __sync_val_compare_and_swap(&value_, 0, 0);// 原子性比较和设置操作 比较value值是否为0 如果为0 则设置为0 返回value修改之前的值
}
T getAndAdd(T x)// x++
{
// in gcc >= 4.7: __atomic_fetch_add(&value_, x, __ATOMIC_SEQ_CST)
return __sync_fetch_and_add(&value_, x);// 原子自增操作 返回未修改的value并将value加上x
}
T addAndGet(T x)// ++x
{
return getAndAdd(x) + x;
}
T incrementAndGet()
{
return addAndGet(1);
}
T decrementAndGet()
{
return addAndGet(-1);
}
void add(T x)
{
getAndAdd(x);
}
void increment()
{
incrementAndGet();
}
void decrement()
{
decrementAndGet();
}
T getAndSet(T newValue)
{
// in gcc >= 4.7: __atomic_exchange_n(&value_, newValue, __ATOMIC_SEQ_CST)
return __sync_lock_test_and_set(&value_, newValue); // 原子赋值操作 返回原来的值 并设置为新值
}
private:
volatile T value_;
/*
volatile 防止编译器对代码进行优化
当要求使用volatile声明的变量的值的时候,系统总是重新从他的所在的内存读取数据,而不是使用寄存器中的备份
*/
};
} // namespace detail
// 实例化 32位 64位整数
typedef detail::AtomicIntegerT<int32_t> AtomicInt32;
typedef detail::AtomicIntegerT<int64_t> AtomicInt64;
} // namespace muduo
#endif // MUDUO_BASE_ATOMIC_H
下面是一个测试程序::
#include "Atomic.h"
#include <assert.h>
#include <iostream>
int main()
{
muduo::AtomicInt64 a0; // 初始值为0
assert(a0.get() == 0); // 获取a0的值 并判断其是否为0
assert(a0.getAndAdd(1) == 0); // a0加1并返回之前的值
assert(a0.get() == 1);
assert(a0.addAndGet(2) == 3); // a0加2并返回加完后的值
assert(a0.get() == 3);
assert(a0.incrementAndGet() == 4); // ++a0
assert(a0.get() == 4);
a0.increment();
assert(a0.get() == 5);
assert(a0.decrementAndGet() == 4);
assert(a0.get() == 4);
a0.decrement();
assert(a0.get() == 3);
std::cout << "Successful ..." << std::endl;
return 0;
}
转载请注明出处