一个关于原子操作的问题及其c++阐述

A question about atomic operation and its c++ exposition

我有一个关于原子操作的问题,以及它在 C++ 中的解释:

std::atomic<int> my_value{0};
 
//...
// executed on thread 1
int a = my_value++;

//...

//executed  on thread 2
int b= my_value++;

对我来说,std::atomic::operator++ 确保 my_value == 2 , 但它是否也确保 {a=0, b=1} 或 {a=1 , b=0} ?

据我所知,只有增量操作是原子的,我以某种方式将此代码视为:

std::atomic<int> my_value{0};
 
//...
// executed on thread 1
int a = m_value;
my_value++;

//...

//executed  on thread 2
int b= my_value;
my_value++;

但是我看到一些实现似乎假设影响和增量都是原子的...为什么,以及如何?

提前感谢您的宝贵时间!

From what I understand only the increment operation is atomic...

错了。 读取实际值和 (post-)increment 都是一个原子操作。与fetch_add(1);相同。请注意操作名称中的“fetch”