Hi!
"Oleksandr" == Oleksandr Byelkin <sanja@askmonty.org> writes:
Oleksandr> Hi! Oleksandr> 10 февр. 2010, в 21:38, Sergei Golubchik написал(а): Oleksandr> [skip]
Why use my_atomic_store32 ?
As I understood idea of atomic operation it is guaranted that we will read consistent value (not one byte from one value and other one from other). Yes I remember your statement that on modern 32bit system you always get it consistent, then why we made atomic operations at all?
Because my_atomic_store32() also adds a full memory barrier to the atomic store operation. That is, if you do
my_atomic_store32(&a, 1); my_atomic_store32(&b, 2);
and then in another thread
if (my_atomic_load32(&b) == 2) { ... here you can be sure that a==1, because a=1 was executed before b=2. And neither compiler nor the cpu swapped two assignments. }
Oleksandr> In other words it is real current value of the variable in all Oleksandr> threads. It looks like what I need. Yes, but if you are only looking at one value, without regard of the other, then you don't need to use my_amtoc_load32() Another problem is that if the variables are related in any way, atomic_load32 is not good enough as you can get 'a' and 'b' from different instances in time. for example: a=b= 0; T1: my_atomic_store32(&a, 1); T1: my_atomic_store32(&b, 2); T2: my_atomic_store32(&a, 3); T2: my_atomic_store32(&b, 4); T3: a1= my_atomic_load32(&a); T3: b1= my_atomic_load32(&b); this means that you can get the following values for a1 and b1: a1= 0 b1= 0 a1= 0 b1= 2 a1= 1 b1= 2 a1= 1 b1= 4 a1= 3 b1= 2 a1= 3 b1= 4 Is this really what you want ? Will your current code work in this senario? Regards, Monty