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

std::atomic::fetch_add

?

(1)

(since C++11) (member only of atomic<Integral> template specialization)

T fetch_add( T arg, std::memory_order order = std::memory_order_seq_cst );

?

T fetch_add( T arg, std::memory_order order = std::memory_order_seq_cst ) volatile;

?

?

(2)

(since C++11) (member only of atomic<T*> template specialization)

T* fetch_add( std::ptrdiff_t arg, std::memory_order order = std::memory_order_seq_cst );

?

T* fetch_add( std::ptrdiff_t arg, std::memory_order order = std::memory_order_seq_cst ) volatile;

?

原子地将当前值替换为值的算术加法的结果和arg.操作是读-修改-写操作.。的值影响内存。order...

签名Integral类型,算法定义为使用两种补码表示形式。没有未定义的结果。为T*类型,结果可能是一个未定义的地址,但否则操作就没有未定义的行为。

参数

arg

-

the other argument of arithmetic addition

order

-

memory order constraints to enforce

返回值

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

例外

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#include <iostream>
#include <thread>
#include <atomic>
 
std::atomic<long long> data;
void do_work()
{
    data.fetch_add(1, std::memory_order_relaxed);
}
 
int main()
{
    std::thread th1(do_work);
    std::thread th2(do_work);
    std::thread th3(do_work);
    std::thread th4(do_work);
    std::thread th5(do_work);
 
    th1.join();
    th2.join();
    th3.join();
    th4.join();
    th5.join();
 
    std::cout << "Result:" << data << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Result:5

二次

另见

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)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com