I know how to use LOCK to thread-safely increment a value:
lock inc [J];
But how do I read [J] (or any value) in a thread-safe manner? The LOCK prefix can’t be used with mov. And if I do the following:
xor eax, eax;
lock add eax, [J];
mov [JC], eax;
It raises an error on line 2.
Use XADD or MOV instruction instead ADD instruction!
See also MFENCE, LFENCE and SFENCE instructions!
EDIT:
You can’t use LOCK instruction with ADD instruction if source operand is a memory operand!
From: "Intel® 64 and IA-32 Architectures Software Developer’s Manual"
EDIT2:
Form: "Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume3A"
So, for reading I prefer to use CMPXCHG instruction with LOCK prefix, like:
For writing:
.