Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen to use Test&Set or Test&Test&Set?
    primarykey
    data
    text
    <p>Parallel programming under x86 can be hard job especially under multi-core CPU. Let say that we have multi-core x86 CPU and more different multithread communication combinations.</p> <ol> <li>Single writer and single reader</li> <li>Single reader multiple writers</li> <li>Multiple readers and single writer</li> <li>Multiple readers and multiple writers</li> </ol> <p>So which one model is better (more efficient) for locking shared memory region: <strong>Test&amp;Set</strong> or <strong>Test&amp;Test&amp;Set</strong> and when to use it!</p> <p>Here I have two simple (no time limited) test procedures written in under Delphi IDE in x86 assembler:</p> <pre><code>procedure TestAndSet(const oldValue, newValue: cardinal; var destination); asm //eax = oldValue //edx = NewLockValue //ecx = destination = 32 bit pointer on lock variable 4 byte aligned @RepeatSpinLoop: push eax //Save lock oldValue (compared) pause //CPU spin-loop hint lock cmpxchg dword ptr [ecx], edx pop eax //Restore eax as oldValue jnz @RepeatSpinLoop //Repeat if cmpxchg wasn't successful end; procedure TestAndTestAndSet(const oldValue, newValue: cardinal; var destination); asm //eax = oldValue //edx = NewLockValue //ecx = destination = 32 bit pointer on lock variable 4 byte aligned @RepeatSpinLoop: push eax //Save lock oldValue (compared) @SpinLoop: pause //CPU spin-loop hint cmp dword ptr [ecx], eax //Test betfore test&amp;set jnz @SpinLoop lock cmpxchg dword ptr [ecx], edx pop eax //Restore eax as oldValue jnz @RepeatSpinLoop //Repeat if cmpxchg wasn't successful end; </code></pre> <p><strong>EDIT:</strong></p> <p>Intel in documentation mention two approach <strong>Test&amp;Set or Test&amp;Test&amp;Set</strong>. I' wont to establish in which case is someone approach better, so when to use it. Check: <a href="http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/cpp/win/compiler_c/intref_cls/common/intref_sse2_pause.htm?wapkw=%28pause%29" rel="nofollow">Intel</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload