CPU count

Is it safe to assume this will work everywhere?

sysconf(_SC_NPROCESSORS_ONLN)

Does the returned value include only physical processors or also logical?
It is portable, as long as you are willing to accept "-1" as a possible answer (EINVAL) and lies from the OS (e.g. 1 when there are multiple CPUs).

I don't know whether it returns physical or logical CPUs (assuming you mean something like "virtual hyperthreaded CPU" here and not "core"). I assume it depends on how the OS interprets its CPU layout for scheduling purposes.
That's fine, I could interpret <1 as 1 (threading disabled). I only concern is that I'll be using that number to spawn threads, so it'd be a problem if I was running more threads than it's actually efficient to run.

Even with bogus info, it's still a lot better than my previous method which involved parsing /proc/cpuinfo. Aside from only working on Linux, I don't trust text files.
Topic archived. No new replies allowed.