Assume I have the following code snippet:
std::atomic<int> a(0);
void thread1()
{
int x = a.fetch_add(1, std::memory_order_relaxed);
std::cout << x << std::endl;
}
void thread2()
{
int x = a.fetch_add(1, std::memory_order_relaxed);
std::cout << x << std::endl;
}
int main()
{
std::thread t1(thread1);
std::thread t2(thread2);
t1.join();
t2.join();
}
The question is: can I obtain 0 0 as a result?
Here both threads read and modify a in a relaxed memory order, so it seems that both of them can see the zero value of a. But in practice I see only 0 1 or 1 0.
Aucun commentaire:
Enregistrer un commentaire