首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::atomic_fetch_add_explicit

Defined in header <atomic>

?

?

?

(1)

(since C++11)

template< class Integral > Integral atomic_fetch_add( std::atomic<Integral>* obj, Integral arg );

?

template< class Integral > Integral atomic_fetch_add( volatile std::atomic<Integral>* obj, Integral arg );

?

?

(2)

(since C++11)

template< class Integral > Integral atomic_fetch_add_explicit( std::atomic<Integral>* obj, Integral arg, std::memory_order order );

?

template< class Integral > Integral atomic_fetch_add_explicit( volatile std::atomic<Integral>* obj, Integral arg, std::memory_order order );

?

?

(3)

(since C++11)

template< class T > T* atomic_fetch_add( std::atomic<T*>* obj, std::ptrdiff_t arg );

?

template< class T > T* atomic_fetch_add( volatile std::atomic<T*>* obj, std::ptrdiff_t arg );

?

?

(4)

(since C++11)

template< class T > T* atomic_fetch_add_explicit( std::atomic<T*>* obj, std::ptrdiff_t arg, std::memory_order order );

?

template< class T > T* atomic_fetch_add_explicit( volatile std::atomic<T*>* obj, std::ptrdiff_t arg, std::memory_order order );

?

执行原子加法。

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

1%29obj->fetch_add(arg)

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

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

3%29obj->fetch_add(arg)

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

参数

obj

-

pointer to the atomic object to modify

arg

-

the value to add to 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 st::原子<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来实现。[医]加。请注意,这个简单的实现并不是没有锁定的。

二次

代码语言:javascript
复制
#include <string>
#include <thread>
#include <vector>
#include <iostream>
#include <atomic>
#include <chrono>
 
// meaning of cnt:
// 10: there are no active readers or writers.
// 1...9: there are 9...1 readers active, The writer is blocked
// 0: temporary value between fetch_sub and fetch_add in reader lock
// -1: there is a writer active. The readers are blocked.
const int N = 10; // nine concurrent readers are allowed
std::atomic<int> cnt = ATOMIC_VAR_INIT(N);
 
std::vector<int> data;
 
void reader(int id)
{
    for(;;)
    {
        // lock
        while(std::atomic_fetch_sub(&cnt, 1) <= 0)
            std::atomic_fetch_add(&cnt, 1);
        // read
        if(!data.empty())
            std::cout << (  "reader " + std::to_string(id)
                          + " sees " + std::to_string(*data.rbegin()) + '\n');
        if(data.size() == 100)
            break;
        // unlock
        std::atomic_fetch_add(&cnt, 1);
        // pause
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
}
 
void writer()
{
    for(int n = 0; n < 100; ++n)
    {
        // lock
        while(std::atomic_fetch_sub(&cnt, N+1) != N)
            std::atomic_fetch_add(&cnt, N+1);
        // write
        data.push_back(n);
        std::cout << "writer pushed back " << n << '\n';
        // unlock
        std::atomic_fetch_add(&cnt, N+1);
        // pause
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
}
 
int main()
{
    std::vector<std::thread> v;
    for (int n = 0; n < N; ++n) {
        v.emplace_back(reader, n);
    }
    v.emplace_back(writer);
    for (auto& t : v) {
        t.join();
    }
}

二次

产出:

二次

代码语言:javascript
复制
writer pushed back 0
reader 8 sees 0
reader 3 sees 0
reader 1 sees 0
<...>
reader 2 sees 99
reader 6 sees 99
reader 1 sees 99

二次

另见

fetch_add

atomically adds the argument to the value stored in the atomic object and obtains the value held previously (public member function of std::atomic)

atomic_fetch_subatomic_fetch_sub_explicit (C++11)(C++11)

subtracts a non-atomic value from an atomic object and obtains the previous value of the atomic (function template)

C原子文档[医]去取[医]加法,原子的[医]去取[医]加[医]显式

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com