std::atomic_fetch_sub, std::atomic_fetch_sub_explicit

定义于头文件 <atomic>
(1) (C++11 起)
template< class Integral >
Integral atomic_fetch_sub( std::atomic<Integral>* obj, Integral arg ) noexcept;
template< class Integral >
Integral atomic_fetch_sub( volatile std::atomic<Integral>* obj, Integral arg ) noexcept;
(2) (C++11 起)
template< class Integral >

Integral atomic_fetch_sub_explicit( std::atomic<Integral>* obj, Integral arg,

                                    std::memory_order order ) noexcept;
template< class Integral >

Integral atomic_fetch_sub_explicit( volatile std::atomic<Integral>* obj, Integral arg,

                                    std::memory_order order) noexcept;
(3) (C++11 起)
template< class T >
T* atomic_fetch_sub( std::atomic<T*>* obj, std::ptrdiff_t arg ) noexcept;
template< class T >
T* atomic_fetch_sub( volatile std::atomic<T*>* obj, std::ptrdiff_t arg ) noexcept;
(4) (C++11 起)
template< class T >

T* atomic_fetch_sub_explicit( std::atomic<T*>* obj, std::ptrdiff_t arg,

                              std::memory_order order ) noexcept;
template< class T >

T* atomic_fetch_sub_explicit( volatile std::atomic<T*>* obj, std::ptrdiff_t arg,

                              std::memory_order order ) noexcept;

进行原子减法。

1-2) 原子地从 obj 所指向的值减去 arg ,并返回 obj 先前保有的值。如同执行下列内容一样进行运算:
1) obj->fetch_sub(arg)
2) obj->fetch_sub(arg, order)
3-4) 原子地减少 obj 所指向的指针值 arg ,并返回 obj 先前保有的值。如同执行下列内容一样进行运算:
3) obj->fetch_sub(arg)
4) obj->fetch_sub(arg, order)

目录

参数

obj - 指向要修改的原子对象的指针
arg - 要从存储于原子对象的值减去的值
order - 此操作所用的内存同步顺序:容许所有值。

返回值

*obj修改顺序中立即前趋此函数效果的值。

可能的实现

版本一
template< class T >
typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value, T>::type
atomic_fetch_sub( std::atomic<T>* obj, T arg );
{
    return obj->fetch_sub(arg);
}
版本二
template< class T >
T* atomic_fetch_sub( std::atomic<T*>* obj, std::ptrdiff_t arg)
{
    return obj->fetch_sub(arg);
}

示例

多个线程可用 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

参阅

原子地从存储于原子对象的值减去参数,并获得先前保有的值
(std::atomic 的公开成员函数)
将非原子值加到原子对象,并获得原子对象的先前值
(函数模板)
atomic_fetch_sub, atomic_fetch_sub_explicitC 文档

版本历史

  • (当前 | 先前 2017年10月15日 (日) 05:37Fruderica讨论 | 贡献. . (4,162字节) (-653). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 16:41P12bot讨论 | 贡献 . . (4,815字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 07:18P12bot讨论 | 贡献 . . (4,815字节) (-321). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:29P12bot讨论 | 贡献 . . (5,136字节) (+281). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 09:22P12讨论 | 贡献 . . (4,855字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (4,855字节) (-58). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 13:13P12讨论 | 贡献 . . (4,913字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (4,913字节) (+4,913). . (Translated from the English version using Google Translate)