I'm experimenting with FASM in order to improve my understanding of concurrency. I've created a program that has two threads each making some number of lock xadd
. I run it on my Win7 64bit on i7 and I'm getting quite interesting results. While the program itself works correctly it loads 4 cores instead of two as I expected.
Task Manager's "Performance" shows clear load of 4 cores
Resource Monitor's, CPU tab shows that my process has two threads
Could someone give a hint on why does that happen? Is there a way to tell which core is currently running a piece of code from my FASM program (just to make sure that the cores are indeed different)?
format PE console
include 'win32ax.inc'
include 'macro32.inc'
entry main
section '.code' code readable executable
main:
invoke CreateThread, 0, 0, atomic_inc, 0, 0, 0
mov [threadHandle], eax
call atomic_inc
invoke WaitForSingleObject, dword[threadHandle], 0xFFFFFFFF
;cinvoke printf, "B - %u", dword [myInt]
invoke system, halt
invoke exit, 0
proc atomic_inc, lpParam
mov ebx, 1000000000
startloop:
cmp ebx, 0
jz endofloop
push ebx
; Loop body
mov eax, 1
lock
xadd dword [myInt], eax
pop ebx
dec ebx
jmp startloop
endofloop:
ret
endp
section '.data' data readable writable
halt db "pause>null",0
myInt dd 0
threadHandle dd 0
section '.idata' import data readable
library msvcrt, 'msvcrt.dll',\
kernel, 'KERNEL32.DLL'
import msvcrt,\
system, 'system',\
printf, 'printf',\
exit, 'exit'
import kernel,\
CreateThread, 'CreateThread',\
WaitForSingleObject, 'WaitForSingleObject',\
Sleep, 'Sleep'