C++11 并发指南六( 类型详解二 std::atomic )介绍了基本的原子类型 std::atomic 的用法,本节我会给大家介绍C++11 标准库中的 std::atomic 针对整形(integral)和指针类型的特化版本做了哪些改进。
总地来说,C++11 标准库中的 std::atomic 针对整形(integral)和指针类型的特化版本新增了一些算术运算和逻辑运算操作。具体如下:
integral fetch_add(integral, memory_order = memory_order_seq_cst) volatile;
integral fetch_add(integral, memory_order = memory_order_seq_cst);
integral fetch_sub(integral, memory_order = memory_order_seq_cst) volatile;
integral fetch_sub(integral, memory_order = memory_order_seq_cst);
integral fetch_and(integral, memory_order = memory_order_seq_cst) volatile;
integral fetch_and(integral, memory_order = memory_order_seq_cst);
integral fetch_or(integral, memory_order = memory_order_seq_cst) volatile;
integral fetch_or(integral, memory_order = memory_order_seq_cst);
integral fetch_xor(integral, memory_order = memory_order_seq_cst) volatile;
integral fetch_xor(integral, memory_order = memory_order_seq_cst);
integral operator++(int) volatile;
integral operator++(int);
integral operator–(int) volatile;
integral operator–(int);
integral operator++() volatile;
integral operator++();
integral operator–() volatile;
integral operator–();
integral operator+=(integral) volatile;
integral operator+=(integral);
integral operator-=(integral) volatile;
integral operator-=(integral);
integral operator&=(integral) volatile;
integral operator&=(integral);
integral operator|=(integral) volatile;
integral operator|=(integral);
integral operator^=(integral) volatile;
integral operator^=(integral);
下面我们来简单介绍以上的 std::atomic 特化版本的成员函数。
fetch_add[table]
[tr][td]if T is integral (1)[/td][td]T fetch_add (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_add (T val, memory_order sync = memory_order_seq_cst) noexcept;
[/td][/tr]
[tr][td]if T is pointer (2)[/td][td]T fetch_add (ptrdiff_t val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_add (ptrdiff_t val, memory_order sync = memory_order_seq_cst) noexcept;
[/td][/tr]
[/table]将原子对象的封装值加 val,并返回原子对象的旧值(适用于整形和指针类型的 std::atomic 特化版本),整个过程是原子的。sync 参数指定内存序:[table]
[tr][td]Memory Order 值[/td][td]Memory Order 类型[/td][/tr]
[tr][td]memory_order_relaxed[/td][td]Relaxed[/td][/tr]
[tr][td]memory_order_consume[/td][td]Consume[/td][/tr]
[tr][td]memory_order_acquire[/td][td]Acquire[/td][/tr]
[tr][td]memory_order_release[/td][td]Release[/td][/tr]
[tr][td]memory_order_acq_rel[/td][td]Acquire/Release[/td][/tr]
[tr][td]memory_order_seq_cst[/td][td]Sequentially consistent[/td][/tr]
[/table]另外,如果第二个参数不指定(取默认参数 memory_order_seq_cst),则 fetch_add 相当与 std::atomic::operator+=。fetch_sub[table]
[tr][td]if T is integral (1)[/td][td]T fetch_sub (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_sub (T val, memory_order sync = memory_order_seq_cst) noexcept;
[/td][/tr]
[tr][td]if T is pointer (2)[/td][td]T fetch_sub (ptrdiff_t val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_sub (ptrdiff_t val, memory_order sync = memory_order_seq_cst) noexcept;
[/td][/tr]
[/table]将原子对象的封装值减 val,并返回原子对象的旧值(适用于整形和指针类型的 std::atomic 特化版本),整个过程是原子的。sync 参数指定内存序:[table]
[tr][td]Memory Order 值[/td][td]Memory Order 类型[/td][/tr]
[tr][td]memory_order_relaxed[/td][td]Relaxed[/td][/tr]
[tr][td]memory_order_consume[/td][td]Consume[/td][/tr]
[tr][td]memory_order_acquire[/td][td]Acquire[/td][/tr]
[tr][td]memory_order_release[/td][td]Release[/td][/tr]
[tr][td]memory_order_acq_rel[/td][td]Acquire/Release[/td][/tr]
[tr][td]memory_order_seq_cst[/td][td]Sequentially consistent[/td][/tr]
[/table]另外,如果第二个参数不指定(取默认参数 memory_order_seq_cst),则 fetch_add 相当与 std::atomic::operator-=。fetch_andT fetch_and (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_and (T val, memory_order sync = memory_order_seq_cst) noexcept;
将原子对象的封装值按位与 val,并返回原子对象的旧值(只适用于整型的 std::atomic 特化版本),整个过程是原子的。sync 参数指定内存序:[table]
[tr][td]Memory Order 值[/td][td]Memory Order 类型[/td][/tr]
[tr][td]memory_order_relaxed[/td][td]Relaxed[/td][/tr]
[tr][td]memory_order_consume[/td][td]Consume[/td][/tr]
[tr][td]memory_order_acquire[/td][td]Acquire[/td][/tr]
[tr][td]memory_order_release[/td][td]Release[/td][/tr]
[tr][td]memory_order_acq_rel[/td][td]Acquire/Release[/td][/tr]
[tr][td]memory_order_seq_cst[/td][td]Sequentially consistent[/td][/tr]
[/table]另外,如果第二个参数不指定(取默认参数 memory_order_seq_cst),则 fetch_add 相当与 std::atomic::operator&=。fetch_orT fetch_or (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_or (T val, memory_order sync = memory_order_seq_cst) noexcept;
将原子对象的封装值按位或 val,并返回原子对象的旧值(只适用于整型的 std::atomic 特化版本),整个过程是原子的。sync 参数指定内存序:[table]
[tr][td]Memory Order 值[/td][td]Memory Order 类型[/td][/tr]
[tr][td]memory_order_relaxed[/td][td]Relaxed[/td][/tr]
[tr][td]memory_order_consume[/td][td]Consume[/td][/tr]
[tr][td]memory_order_acquire[/td][td]Acquire[/td][/tr]
[tr][td]memory_order_release[/td][td]Release[/td][/tr]
[tr][td]memory_order_acq_rel[/td][td]Acquire/Release[/td][/tr]
[tr][td]memory_order_seq_cst[/td][td]Sequentially consistent[/td][/tr]
[/table]另外,如果第二个参数不指定(取默认参数 memory_order_seq_cst),则 fetch_add 相当与 std::atomic::operator|=。fetch_xorT fetch_xor (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T fetch_xor (T val, memory_order sync = memory_order_seq_cst) noexcept;
将原子对象的封装值按位异或 val,并返回原子对象的旧值(只适用于整型的 std::atomic 特化版本),整个过程是原子的。sync 参数指定内存序:[table]
[tr][td]Memory Order 值[/td][td]Memory Order 类型[/td][/tr]
[tr][td]memory_order_relaxed[/td][td]Relaxed[/td][/tr]
[tr][td]memory_order_consume[/td][td]Consume[/td][/tr]
[tr][td]memory_order_acquire[/td][td]Acquire[/td][/tr]
[tr][td]memory_order_release[/td][td]Release[/td][/tr]
[tr][td]memory_order_acq_rel[/td][td]Acquire/Release[/td][/tr]
[tr][td]memory_order_seq_cst[/td][td]Sequentially consistent[/td][/tr]
[/table]另外,如果第二个参数不指定(取默认参数 memory_order_seq_cst),则 fetch_add 相当与 std::atomic::operator^=。operator++[table]
[tr][td]pre-increment (1)[/td][td]T operator++() volatile noexcept;
T operator++() noexcept;
[/td][/tr]
[tr][td]post-increment (2)[/td][td]T operator++ (int) volatile noexcept;
T operator++ (int) noexcept;
[/td][/tr]
[/table]自增运算符重载, 第一种形式 (1) 返回自增后的值(即前缀++),第二种形式(2) 返回自增前的值(即后缀++),适用于整形和指针类型的 std::atomic 特化版本。operator–自减运算符重载, 第一种形式 (1) 返回自减后的值(即前缀–),第二种形式(2) 返回自减前的值(即后缀–),适用于整形和指针类型的 std::atomic 特化版本。atomic::operator (comp. assign.)复合赋值运算符重载,主要包含以下形式:
[table]
[tr][td]if T is integral (1)[/td][td]T operator+= (T val) volatile noexcept;
T operator+= (T val) noexcept;
T operator-= (T val) volatile noexcept;
T operator-= (T val) noexcept;
T operator&= (T val) volatile noexcept;
T operator&= (T val) noexcept;
T operator|= (T val) volatile noexcept;
T operator|= (T val) noexcept;
T operator^= (T val) volatile noexcept;
T operator^= (T val) noexcept;
[/td][/tr]
[tr][td]if T is pointer (2)[/td][td]T operator+= (ptrdiff_t val) volatile noexcept;
T operator+= (ptrdiff_t val) noexcept;
T operator-= (ptrdiff_t val) volatile noexcept;
T operator-= (ptrdiff_t val) noexcept;
[/td][/tr]
[/table]以上各个 operator 都会有对应的fetch_*操作,详细见下表:
[table]
[tr][td=1,2]操作符[/td][td=2,1]成员函数[/td][td=3,1]支持类型[/td][/tr]
[tr][td]复合赋值[/td][td]等价于[/td][td]整型[/td][td]指针类型[/td][td]其他类型[/td][/tr]
[tr][td]+[/td][td]atomic::operator+=[/td][td]atomic::fetch_add[/td][td]是[/td][td]是[/td][td]否[/td][/tr]
[tr][td]-[/td][td]atomic::operator-=[/td][td]atomic::fetch_sub[/td][td]是[/td][td]是[/td][td]否[/td][/tr]
[tr][td]&[/td][td]atomic::operator&=[/td][td]atomic::fetch_and[/td][td]是[/td][td]否[/td][td]否[/td][/tr]
[tr][td]|[/td][td]atomic::operator|=[/td][td]atomic::fetch_or[/td][td]是[/td][td]否[/td][td]否[/td][/tr]
[tr][td][1][td]atomic::operator^=[/td][td]atomic::fetch_xor[/td][td]是[/td][td]否[/td][td]否[/td][/tr]
[/table]好了,本节先介绍这里,下一节我会介绍 C++11 中 C 风格的原子操作 API。
本文链接:http://www.cnblogs.com/haippy/p/3304556.html,转载请注明。
/td ↩︎