1

Possible Duplicate:
Programmatically find the number of cores on a machine

What is the POSIX or x86, x86-64 specific system call to determine the max number of threads the system can run without over-subscription? Thank you.

Community
  • 1
  • 1
pic11
  • 14,267
  • 21
  • 83
  • 119
  • 1
    [`thread::hardware_concurrency()`](http://www.boost.org/doc/libs/1_37_0/doc/html/thread/thread_management.html#thread.thread_management.thread.hardware_concurrency) essentially tells you the number of processor cores running. [Refer to this Stack Overflow answer to figure the number of CPUs available on a variety of platforms](http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine/150971#150971). – In silico Sep 07 '11 at 22:18
  • Unless you know you're the only process running, and that all your threads will maintain full load, the "number of threads you can run without over-subscription" is not a very well-defined concept, and can vary dynamically. – R.. GitHub STOP HELPING ICE Sep 07 '11 at 22:59

1 Answers1

5

It uses C-compatible constructs, so why not just use the actual code? [libs/thread/src/*/thread.cpp]

using pthread library:

unsigned thread::hardware_concurrency()
{
#if defined(PTW32_VERSION) || defined(__hpux)
    return pthread_num_processors_np();
#elif defined(__APPLE__) || defined(__FreeBSD__)
    int count;
    size_t size=sizeof(count);
    return sysctlbyname("hw.ncpu",&count,&size,NULL,0)?0:count;
#elif defined(BOOST_HAS_UNISTD_H) && defined(_SC_NPROCESSORS_ONLN)
    int const count=sysconf(_SC_NPROCESSORS_ONLN);
    return (count>0)?count:0;
#elif defined(_GNU_SOURCE)
    return get_nprocs();
#else
    return 0;
#endif
}

in windows:

unsigned thread::hardware_concurrency()
{
    SYSTEM_INFO info={{0}};
    GetSystemInfo(&info);
    return info.dwNumberOfProcessors;
}
Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • For the header files: http://stackoverflow.com/questions/27981038/error-get-nprocs-was-not-declared-in-this-scope – gsamaras Jan 16 '15 at 09:54
  • This is the number of logical cores, not the number of physical cores, so technically the threads would still be oversubscribed. – Arran Cudbard-Bell Sep 16 '21 at 19:29