Muduo Atomic Operations: Efficient and Thread-Safe Integer Management
// 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
{
// This is a namespace for implementation details of Atomic operations.
template
// Get the current value of the atomic integer. T get() { return _sync_val_compare_and_swap(&value, 0, 0); }
// Atomically add 'x' to the current value and return the previous value. T getAndAdd(T x) { return _sync_fetch_and_add(&value, x); }
// Atomically add 'x' to the current value and return the new value. T addAndGet(T x) { return getAndAdd(x) + x; }
// Atomically increment the current value by 1 and return the new value. T incrementAndGet() { return addAndGet(1); }
// Atomically decrement the current value by 1 and return the new value. T decrementAndGet() { return addAndGet(-1); }
// Atomically add 'x' to the current value. void add(T x) { getAndAdd(x); }
// Atomically increment the current value by 1. void increment() { incrementAndGet(); }
// Atomically decrement the current value by 1. void decrement() { decrementAndGet(); }
// Atomically set the current value to 'newValue' and return the previous value. T getAndSet(T newValue) { return _sync_lock_test_and_set(&value, newValue); }
private: // The value of the atomic integer. volatile T value_; }; }
// Typedefs for AtomicInt32 and AtomicInt64. typedef detail::AtomicIntegerT<int32_t> AtomicInt32; typedef detail::AtomicIntegerT<int64_t> AtomicInt64; }
#endif // MUDUO_BASE_ATOMIC_H
原文地址: https://www.cveoy.top/t/topic/nnoP 著作权归作者所有。请勿转载和采集!