// This is a namespace for implementation details of Atomic operations. namespace muduo {

namespace detail { // This is a template class for an atomic integer of type T. template class AtomicIntegerT : boost::noncopyable { public: // Constructor. AtomicIntegerT() : value_(0) { }

// 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; }

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#inc

原文地址: https://www.cveoy.top/t/topic/b3jv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录