just::thread Complete C++ Standard Thread Library by Just Software Solutions

Documentation Home >> Headers >> <atomic> Header >> std::atomic class template

The std::atomic class provides a wrapper with atomic operations for any type that satisfies the requirements below.

The template parameter BaseType must:

  • have a trivial default constructor
  • have a trivial copy-assignment operator
  • have a trivial destructor
  • be bitwise-equality-comparable

This basically means that std::atomic<some-built-in-type> is fine, as is std::atomic<some-simple-struct>, but things like std::atomic<std::string> are not.

In addition to the primary template, there are specializations for the built-in integral types and pointers to provide additional operations such as x++.

Instances of std::atomic are not CopyConstructible or CopyAssignable, as these operations cannot be performed as a single atomic operation.

template<typename BaseType>
struct atomic
{
    atomic() = default;
    constexpr atomic(BaseType);
    BaseType operator=(BaseType) volatile;

    atomic(const atomic&) = delete;
    atomic& operator=(const atomic&) = delete;

    bool is_lock_free() const volatile;
    void store(
        BaseType,
        memory_order = memory_order_seq_cst) volatile;
    BaseType load(
        memory_order = memory_order_seq_cst)
        const volatile;
    BaseType exchange(
        BaseType,memory_order = memory_order_seq_cst)
        volatile;

    bool compare_exchange_strong(
        BaseType & old_value, BaseType new_value,
        memory_order order = memory_order_seq_cst)
        volatile;
    bool compare_exchange_strong(
        BaseType & old_value, BaseType new_value,
        memory_order success_order,
        memory_order failure_order) volatile;
    bool compare_exchange_weak(
        BaseType & old_value, BaseType new_value,
        memory_order order = memory_order_seq_cst)
        volatile;
    bool compare_exchange_weak(
        BaseType & old_value, BaseType new_value,
        memory_order success_order,
        memory_order failure_order) volatile;

    operator BaseType () const volatile;
};

template<typename BaseType>
bool atomic_is_lock_free(
    volatile const atomic<BaseType>*);
template<typename BaseType>
void atomic_init(
    volatile atomic<BaseType>*, void*);
template<typename BaseType>
BaseType atomic_exchange(
    volatile atomic<BaseType>*, memory_order);
template<typename BaseType>
BaseType atomic_exchange_explicit(
    volatile atomic<BaseType>*, memory_order);
template<typename BaseType>
void atomic_store(
    volatile atomic<BaseType>*, memory_order);
template<typename BaseType>
void atomic_store_explicit(
    volatile atomic<BaseType>*, memory_order);
template<typename BaseType>
BaseType atomic_load(
    volatile const atomic<BaseType>*);
template<typename BaseType>
BaseType atomic_load_explicit(
    volatile const atomic<BaseType>*, memory_order);
template<typename BaseType>
bool atomic_compare_exchange_strong(
    volatile atomic<BaseType>*,BaseType * old_value,
    BaseType new_value);
template<typename BaseType>
bool atomic_compare_exchange_strong_explicit(
    volatile atomic<BaseType>*,BaseType * old_value,
    BaseType new_value, memory_order success_order,
    memory_order failure_order);
template<typename BaseType>
bool atomic_compare_exchange_weak(
    volatile atomic<BaseType>*,BaseType * old_value,
    BaseType new_value);
template<typename BaseType>
bool atomic_compare_exchange_weak_explicit(
    volatile atomic<BaseType>*,BaseType * old_value,
    BaseType new_value, memory_order success_order,
    memory_order failure_order);
Header

#include <atomic>

See Also