threading question lazarus / 21-General ---------------------------------------------------------------------- anonymousstranger · 2021-01-24 03:42 I'm studying the documentation concerning hyperthreading, and have a question about one of the examples given: From(edited, previously wrong link): https://wiki.freepascal.org/Example_of_multi- threaded_application:_array_of_threads Given: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetLogicalCpuCount: integer;// returns a good default for the number of threads on this system//returns total number of processors available to system including logical hyperthreaded processorsvar  i: Integer;  ProcessAffinityMask, SystemAffinityMask: DWORD_PTR;  Mask: DWORD;  SystemInfo: SYSTEM_INFO;begin  if GetProcessAffinityMask(GetCurrentProcess, ProcessAffinityMask, SystemAffinityMask)  then begin    Result := 0;    for i := 0 to 31 do begin      Mask := DWord(1) shl i;      if (ProcessAffinityMask and Mask)<>0 then        inc(Result);    end;  end else begin    //can't get the affinity mask so we just report the total number of processors    GetSystemInfo(SystemInfo);    Result := SystemInfo.dwNumberOfProcessors;  end;end;  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)? ---------------------------------------------------------------------- PascalDragon · 2021-01-24 11:35 Quote from: anonymousstranger on January 24, 2021, 04:42:08 amWhy 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. ---------------------------------------------------------------------- anonymousstranger · 2021-01-25 01:15 Thanks for the clarification, and I edited the link. Must've copied the wrong window as I often have multiples open at any given time to cross-reference and multi task. Also thanks for the link you provided.