Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there standard way to detect number of CPU's?

41 views
Skip to first unread message

Melzzzzz

unread,
Aug 4, 2015, 11:48:23 AM8/4/15
to
I can't seem to find standard way, is there one?

Victor Bazarov

unread,
Aug 4, 2015, 11:53:01 AM8/4/15
to
On 8/4/2015 11:48 AM, Melzzzzz wrote:
> I can't seem to find standard way, is there one?
>

Nope. It's platform-specific, I am pretty sure.

V
--
I do not respond to top-posted replies, please don't ask

Öö Tiib

unread,
Aug 4, 2015, 12:09:56 PM8/4/15
to
On Tuesday, 4 August 2015 18:48:23 UTC+3, Melzzzzz wrote:
> I can't seem to find standard way, is there one?

With C++11:

#include <thread>
unsigned int nthreads = std::thread::hardware_concurrency();

Without C++11 but boost libraries available:

#include <boost/thread.hpp>
unsigned int nthreads = boost::thread::hardware_concurrency();

For old crap no C++11, no boost there are typically nonstandard
ways per each platform like 'sysctl', 'sysconf' or 'GetSystemInfo'.

Victor Bazarov

unread,
Aug 4, 2015, 12:12:01 PM8/4/15
to
On 8/4/2015 12:09 PM, Öö Tiib wrote:
> On Tuesday, 4 August 2015 18:48:23 UTC+3, Melzzzzz wrote:
>> I can't seem to find standard way, is there one?
>
> With C++11:
>
> #include <thread>
> unsigned int nthreads = std::thread::hardware_concurrency();

But that doesn't actually give the number of CPUs, does it? And the
implementation is allowed to return 0 (which isn't helpful to those who
need to know the number of CPUs)...

>
> Without C++11 but boost libraries available:
>
> #include <boost/thread.hpp>
> unsigned int nthreads = boost::thread::hardware_concurrency();
>
> For old crap no C++11, no boost there are typically nonstandard
> ways per each platform like 'sysctl', 'sysconf' or 'GetSystemInfo'.
>

Öö Tiib

unread,
Aug 4, 2015, 12:43:54 PM8/4/15
to
On Tuesday, 4 August 2015 19:12:01 UTC+3, Victor Bazarov wrote:
> On 8/4/2015 12:09 PM, Öö Tiib wrote:
> > On Tuesday, 4 August 2015 18:48:23 UTC+3, Melzzzzz wrote:
> >> I can't seem to find standard way, is there one?
> >
> > With C++11:
> >
> > #include <thread>
> > unsigned int nthreads = std::thread::hardware_concurrency();
>
> But that doesn't actually give the number of CPUs, does it? And the
> implementation is allowed to return 0 (which isn't helpful to those who
> need to know the number of CPUs)...

Yes, it gives number of threads that can run concurrently and that may
be more than number of CPU-s since modern CPU can be hyperthreadable
unit. CPU becomes all more and more dim concept and so the number of
those may be is not what we actually want? If it really does return 0 then
there are the other options below.

Additionally all more and more C++ compilers support OpenMP and
that brings 'omp_get_num_procs' with it. It does return processors
available to the calling process at moment of calling. That may be also
something of interest for a running program.

Ian Collins

unread,
Aug 4, 2015, 5:12:54 PM8/4/15
to
Victor Bazarov wrote:
> On 8/4/2015 12:09 PM, Öö Tiib wrote:
>> On Tuesday, 4 August 2015 18:48:23 UTC+3, Melzzzzz wrote:
>>> I can't seem to find standard way, is there one?
>>
>> With C++11:
>>
>> #include <thread>
>> unsigned int nthreads = std::thread::hardware_concurrency();
>
> But that doesn't actually give the number of CPUs, does it? And the
> implementation is allowed to return 0 (which isn't helpful to those who
> need to know the number of CPUs)...

While true, the number returned (if it isn't zero) if probably more use
than the number of CPUs which is becoming more of a nebulous concept
these days!

--
Ian Collins
0 new messages