在线文档教程
C++

std::atomic_fetch_sub_explicit

STD::原子[医]去取[医]分,STD::原子[医]去取[医]分[医]显式

Defined in header
(1)(since C++11)
template< class Integral > Integral atomic_fetch_sub( std::atomic<Integral>* obj, Integral arg
template< class Integral > Integral atomic_fetch_sub( volatile std::atomic<Integral>* obj, Integral arg
(2)(since C++11)
template< class Integral > Integral atomic_fetch_sub_explicit( std::atomic<Integral>* obj, Integral arg, std::memory_order order
template< class Integral > Integral atomic_fetch_sub_explicit( volatile std::atomic<Integral>* obj, Integral arg, std::memory_order order
(3)(since C++11)
template< class T > T* atomic_fetch_sub( std::atomic<T*>* obj, std::ptrdiff_t arg
template< class T > T* atomic_fetch_sub( volatile std::atomic<T*>* obj, std::ptrdiff_t arg
(4)(since C++11)
template< class T > T* atomic_fetch_sub_explicit( std::atomic<T*>* obj, std::ptrdiff_t arg, std::memory_order order
template< class T > T* atomic_fetch_sub_explicit( volatile std::atomic<T*>* obj, std::ptrdiff_t arg, std::memory_order order

执行原子减法。

1-2%29arg从所指的价值obj并返回值。obj以前持有。执行该操作时,就像执行了以下操作一样:

1%29obj->fetch_sub(arg)

2%29obj->fetch_sub(arg, order)

3-4%29原子地减少指针值,obj,通过arg,并返回值obj以前持有。执行该操作时,就像执行了以下操作一样:

3%29obj->fetch_sub(arg)

4%29obj->fetch_sub(arg, order)

参数

obj-pointer to the atomic object to modify
arg-the value to subtract from the value stored in the atomic object
order-the memory sycnhronization ordering for this operation: all values are permitted.

返回值

中此函数的效果之前的值。修改顺序成*obj...

例外

noexcept规格:

noexcept

可能的实施

第一版

*。

模板<class T>TypeName STD::Enable[医]如果<std::is[医]积分<T>*价值&%21 std::is[医]相同<T,bool>::value,T>:type原子[医]去取[医]分%28 STD::原子<T>%2AOBJ,T Arg%29;{返回obj->提取[医]次%28 arg%29;}

第二版

模板<类T>T%2A原子[医]去取[医]分%28 STD::原子<T%2A>%2AOBJ,STD::ptrdiff[医]t arg%29{返回obj->获取[医]次%28 arg%29;}

多线程可以使用fetch_sub并发处理索引容器。

二次

#include <string> #include <thread> #include <vector> #include <iostream> #include <atomic> #include <numeric> const int N = 10000; std::atomic<int> cnt; std::vector<int> data(N void reader(int id) { for (;;) { int idx = atomic_fetch_sub_explicit(&cnt, 1, std::memory_order_relaxed if (idx >= 0) { std::cout << "reader " << std::to_string(id) << " processed item " << std::to_string(data[idx]) << '\n'; } else { std::cout << "reader " << std::to_string(id) << " done\n"; break; } } } int main() { std::iota(data.begin(), data.end(), 1 cnt = data.size() - 1; std::vector<std::thread> v; for (int n = 0; n < 10; ++n) { v.emplace_back(reader, n } for (auto& t : v) { t.join( } }

二次

产出:

二次

reader 2 processed item 10000 reader 6 processed item 9994 reader 4 processed item 9996 reader 6 processed item 9992 <....> reader 0 done reader 5 done reader 3 done reader 9 done

二次

另见

fetch_subatomically subtracts the argument from the value stored in the atomic object and obtains the value held previously (public member function of std::atomic)
atomic_fetch_addatomic_fetch_add_explicit (C++11)(C++11)adds a non-atomic value to an atomic object and obtains the previous value of the atomic (function template)

C原子文档[医]去取[医]亚原子的[医]去取[医]分[医]显式

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/原子/原子[医]去取[医]分