If I have 2 threads and in main function, I init a variable x like this
std::atomic<int> x=0;,
In thread 1 I do this:
while(true){
x++;
}
And In thread 2 I do this:
y=++x;
my question is that:is there any possibility that variable y can get the wrong number?
I mean for example:
if in thread2,at that moment,x=2;then because of "++x",x=3,so I hope y=3;
But I am afraid that between "++x" and "y=x", thread 1 will rewrite x again, so I may have y=4 or something.
atomicbecause your question is aboutynotx. If you have two threads andx=0at the beginning, at the end,xwill be 2 butywill depend on the order (i.e) you can't knowyvalue unless you don't know the order.