文档库

最新最全的文档下载
当前位置:文档库 > C++11 并发指南六( atomic 类型详解二 stdatomic )

C++11 并发指南六( atomic 类型详解二 stdatomic )

std::atomic 基本介绍

std::atomic 是模板类,一个模板类型为T 的原子对象中封装了一个类型为T 的值。template struct atomic;

原子类型对象的主要特点就是从不同线程访问不会导致数据竞争(data race)。因此从不同线程访问某个原子对象是良性(well-defined) 行为,而通常对于非原子类型而言,并发访问某个对象(如果不做任何同步操作)会导致未定义(undifined) 行为发生。

C++11 标准中的基本std::atomic 模板定义如下:

C++11 并发指南六( atomic 类型详解二 stdatomic )

template < class T > struct atomic {

bool is_lock_free() const volatile;

bool is_lock_free() const;

void store(T, memory_order = memory_order_seq_cst) volatile;

void store(T, memory_order = memory_order_seq_cst);

T load(memory_order = memory_order_seq_cst) const volatile;

T load(memory_order = memory_order_seq_cst) const;

operator T() const volatile;

operator T() const;

T exchange(T, memory_order = memory_order_seq_cst) volatile;

T exchange(T, memory_order = memory_order_seq_cst);

bool compare_exchange_weak(T &, T, memory_order, memory_order) volat ile;

bool compare_exchange_weak(T &, T, memory_order, memory_order);

bool compare_exchange_strong(T &, T, memory_order, memory_order) vola tile;

bool compare_exchange_strong(T &, T, memory_order, memory_order);

bool compare_exchange_weak(T &, T, memory_order = memory_order_seq _cst) volatile;

bool compare_exchange_weak(T &, T, memory_order = memory_order_seq _cst);

bool compare_exchange_strong(T &, T, memory_order = memory_order_se q_cst) volatile;

bool compare_exchange_strong(T &, T, memory_order = memory_order_se q_cst);

atomic() = default;

constexpr atomic(T);

atomic(const atomic &) = delete;

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

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

T operator=(T) volatile;

T operator=(T);

};

千炮捕鱼 新传奇私服