multithreading - Adding a synchronization point in x86 assembly -
i have have way have 4 threads run point @ same time. example
thread 1 : mov eax,ebx, mov ecx, edx, [s], mov eax, edx, ... thread 2: sbb eax,ebx, [s], mov ecx, edx, ... thread 3: mov eax,ebx, xchg eax,ebx, cmp edx, ecx, [s], mov eax, ebx, ... thread 4: dec eax, sub eax,ecx, [s], .... [s] place holder 'synchronization point'. after threads have reached point, should start @ same time. how do this?
the code have like
number_of_threads 4 temp: dd 0 ;a 'synchronization variable' thread 1 code
;synchronization [s] lock add [temp],0x1 wloop1: cmp [temp], number_of_threads jne wloop1 thread 2 code
;synchronization [s] lock add [temp],0x1 wloop2: cmp [temp], number_of_threads jne wloop2 thread 3 code
;synchronization [s] lock add [temp],0x1 wloop3: cmp [temp], number_of_threads jne wloop3 thread 4 code
;synchronization [s] lock add [temp],0x1 wloop4: cmp [temp], number_of_threads jne wloop4 this way make sure threads reach [s] , start off there @ same time. code follows [s] executes if temp becomes number_of_threads there problem code such race? not sure if way this.
that's 1 way it, , don't see race condition. sure ties threads, though, busy waiting. not bad if wait expected brief, waits longer millisecond or so, should use os-supplied synchronization primitive. spinning loop while waiting eats cpu cycles candy, , you're going notice performance problem if waits long.
on windows, you'd use synchronization barrier. there's analogous in linux world. can't sure, since i'm not familiar linux programming.
you might interested in x86 pause instruction, reduce cpu load. this answer has description.
Comments
Post a Comment