BSD cpucount was Re: [fpc-devel] Feature announcement: Extension of TThread's interface

Marco van de Voort marcov at
Thu Dec 27 21:17:39 CET 2012

In our previous episode, Sven Barth said:

> - ProcessorCount (class):
>      Returns the count of CPU cores detected by the RTL. This is based 
> on the new global property System.GetCPUCount which needs to be 
> implemented per target. Currently only a default implementation exists 
> which returns "1".

Here is some BSD code. I don't know what it returns, physical or virtual
cores since the machine I developed it with has no physical ones.

The hw.* mib might vary slightly with the BSD flavour. The pchar casts are
probably due to a bug in the FPC sysctl unit.

uses baseunix,sysctl,ctypes;

var mib : array[0..3] of cint;
    len : cint;

len:= sizeof(numCPU); 

{ set the mib for hw.ncpu }
mib[0] := CTL_HW;
mib[1] := HW_NCPU;  // alternatively, try HW_AVAILCPU;

{ get the number of CPUs from the system }
fpsysctl(pchar(@mib), 2, @numCPU, @len, NIL, 0);

if( numCPU < 1 ) then 
     mib[1] := HW_NCPU;
     fpsysctl( pchar(@mib), 2, @numCPU, @len, NIL, 0 );

     if( numCPU < 1 ) then
          numCPU := 1;


More information about the fpc-devel mailing list