1 原子性操作
// 原子自增操作
type __sync_fetch_and_add (type *ptr, type value)
// 原子比较和交换(设置)操作
type __sync_val_compare_and_swap (type *ptr, type oldval type newval)
bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval)
// 原子赋值操作
type __sync_lock_test_and_set (type *ptr, type value)
使用这些原子性操作,编译的时候需要加-march=cpu-type
2 类实现源文件
muduo/base/Atomic.h
// 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 <boost/noncopyable.hpp>
#include <stdint.h>
namespace muduo
{
namespace detail
{
template<typename T>
class AtomicIntegerT : boost::noncopyable
{
public:
AtomicIntegerT()
: value_(0)
{
}
// uncomment if you need copying and assignment
/