Concurrent programming under x86 can be difficult, especially on multi-core processors. Let's say that we have a multi-core x86 processor and more different combinations of multi-threaded communications.
- Single writer and single reader
- Several writers with one reader
- Multiple readers and single writer
- Several readers and several authors
So, which one model is better (more efficient) for locking the shared memory area: Test & Set or Test & Test & Set and when to use it!
Here I have two simple (no time limits) test procedures written in the Delphi IDE in x86 assembler:
procedure TestAndSet(const oldValue, newValue: cardinal; var destination);
asm
@RepeatSpinLoop:
push eax
pause
lock cmpxchg dword ptr [ecx], edx
pop eax
jnz @RepeatSpinLoop
end;
procedure TestAndTestAndSet(const oldValue, newValue: cardinal; var destination);
asm
@RepeatSpinLoop:
push eax
@SpinLoop:
pause
cmp dword ptr [ecx], eax
jnz @SpinLoop
lock cmpxchg dword ptr [ecx], edx
pop eax
jnz @RepeatSpinLoop
end;
EDIT:
Intel Test & Set Test & Test & Set. , - , , . : Intel