Home > OS >  Does WinAPI have a function that reports on the number of processors available for current process?
Does WinAPI have a function that reports on the number of processors available for current process?

Time:11-18

I know that with the function SetProcessAffinityMask(1) I can set a mask that defines the available processors (e.g. 1 - only the first processor is available). I can count the number of 1's in the binary version of AffinityMask and get the result, but I wonder if there exists a special function that can show it.

CodePudding user response:

GetProcessAffinityMask returns values for the process and system but you have to count the bits yourself. Maximum is 32 or 64 depending on your processes bitness.

GetNativeSystemInfo is documented to return the number of processors for the group your process is in. This means the maximum number is probably 64.

There can be more than 64 logical processors on Windows 7 and later but then you have to deal with NUMA and processor groups. GetActiveProcessorGroupCount GetActiveProcessorCount should be able to tell you the number for the system. You can call some extra functions to spread your threads over multiple groups but according to MSDN:

Starting with Windows 11 and Windows Server 2022, on a system with more than 64 processors, process and thread affinities span all processors in the system, across all processor groups, by default.

  • Related