Recent

Author Topic: threading question  (Read 755 times)

anonymousstranger

  • New Member
  • *
  • Posts: 49
threading question
« on: January 24, 2021, 04:42:08 am »
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][+][-]
  1. function GetLogicalCpuCount: integer;
  2. // returns a good default for the number of threads on this system
  3. //returns total number of processors available to system including logical hyperthreaded processors
  4. var
  5.   i: Integer;
  6.   ProcessAffinityMask, SystemAffinityMask: DWORD_PTR;
  7.   Mask: DWORD;
  8.   SystemInfo: SYSTEM_INFO;
  9. begin
  10.   if GetProcessAffinityMask(GetCurrentProcess, ProcessAffinityMask, SystemAffinityMask)
  11.   then begin
  12.     Result := 0;
  13.     for i := 0 to 31 do begin
  14.       Mask := DWord(1) shl i;
  15.       if (ProcessAffinityMask and Mask)<>0 then
  16.         inc(Result);
  17.     end;
  18.   end else begin
  19.     //can't get the affinity mask so we just report the total number of processors
  20.     GetSystemInfo(SystemInfo);
  21.     Result := SystemInfo.dwNumberOfProcessors;
  22.   end;
  23. end;
  24.  

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)?
« Last Edit: January 25, 2021, 02:11:22 am by anonymousstranger »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: threading question
« Reply #1 on: January 24, 2021, 12:35:15 pm »
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.

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: threading question
« Reply #2 on: January 25, 2021, 02:15:37 am »
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.  :)

 

TinyPortal © 2005-2018