Code: Select all
;Sets the CPU affinity, i.e. which CPU(s) are working on a process.
;To verify it, open the Task Manager, right click a process and check for the affinity there
;The affinity is a binary mask, so 1=CPU0,2=CPU1,4=CPU2,8=CPU3 and so on.
;Add the values for the CPUs you want to use to get the mask, like
;9=8+1=CPU3+CPU0
;Max. - forum.purebasic.com - 07/28/2005
#PROCESS_ALL_ACCESS=$1F0FFF 
lpProcessAffinityMask.l
lpSystemAffinityMask.l
Program.s = "Window Title of the Application"
CustomAffinity = 1 ;run on CPU0.
hWnd = FindWindow_ (0,Program)
result = GetWindowThreadProcessId_(hWnd,@pid)
pHandle = OpenProcess_(#PROCESS_ALL_ACCESS, #False, pid)
;Result = GetProcessAffinityMask_(pHandle,@lpProcessAffinityMask,@lpSystemAffinityMask)
;The default should be all CPU work on a process, so hyperthreading CPUs should return 3 (CPU0+virtual CPU1)
;The System Affinity Mask basically is the number of CPUs, aka the maximum. You can do some bitshifting if
;you want to retrieve the number of CPUs.
Debug lpProcessAffinityMask
Debug lpSystemAffinityMask
;Now specify the CPU the process shall run on
Result = SetProcessAffinityMask_ (pHandle,CustomAffinity)
;a very intersting variant of this is
;SetThreadAffinityMask_(threadHandle,dwThreadAffinityMask)
;which allows to spread different threads to specific CPUs in order to maximize Performance in multiprocessor
;systems


