Closed dmik closed 5 years ago
Libc already can return the number of CPU's. I forget the specifics but IIRC it was in /sys/sysctrl.h, HW_NCPU. Example code from libav (similar in FFmpeg) that works (after configure defines some stuff)
#elif HAVE_SYSCTL && defined(HW_NCPU)
int mib[2] = { CTL_HW, HW_NCPU };
size_t len = sizeof(nb_cpus);
if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
nb_cpus = 0;
#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
nb_cpus = sysconf(_SC_NPROC_ONLN);
#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
#endif
Yes, that's true, but _SC_NPROCESSORS_ONLN (which is used more often, in my practice) isn't implemented yet.
Note that above I also changed the sysctl (HW_NCPU)
implementation to just call sysconf (_SC_NPROCESSORS_ONLN)
so that the underlying code is defined only once.
This is used in many Unix software to determine the number of CPUs (which is then used to run several tasks on multiple cores in parallel). Most significant examples are Qt (see https://github.com/bitwiseworks/qtbase-os2/issues/8) and Python.
Implementing is as trivial as using
DosQuerySysInfo(QSV_NUMPROCESSORS)
insrc/emx/src/lib/misc/sysconf.c
.