Why is the upper bound of i set to 31 in the for loop? Seems like its going to report 32 threads even if the number of potential threads on the system exceeds 32, which is really likely given AMD's recent advances. Is this an arbitrary upper bound, or am I missing something? Why even fool with ProcessAffinityMask when you can just GetSystemInfo, and potentially get more accurate results (or so it seems)?
The example probably originated from 32-bit times where
DWORD_PTR has a width of 4 Byte (thus 32-bits and thus the upper bound). On a 64-bit system
DWORD_PTR has a size of 64-bit thus the loop should run from 0 to 63 (but only on 64-bit systems!). But even then you need to take care of systems with more than 64 processors (see
here).
In essence the use
GetProcessAffinityMask to determine the number of logical processors is in my opinion wrong as it only returns the number of processors that are currently available for the process (though for the intended purpose of "default for the number of threads on this system" it should be okay).
For example
System.CPUCount does use the
GetSystemInfo on Windows.
Also it seems that your link is wrong.