Is there a way to determine how many cores a machine has from C/C++ in a platform-independent way? If no such thing exists, what about determining it per-platform (Windows/*nix/Mac)?
C++ – Programmatically find the number of cores on a machine
cmultiplatformmultithreading
Related Topic
- C++ – What does the explicit keyword mean
- C++ – How to iterate over the words of a string
- C++ – The Definitive C++ Book Guide and List
- Do I cast the result of malloc
- C++ – the “–>” operator in C/C++
- Sqlite – Improve INSERT-per-second performance of SQLite
- C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming
- C# – System.Windows.Application.GetResourceStream returns null
Best Answer
C++11
Reference: std::thread::hardware_concurrency
In C++ prior to C++11, there's no portable way. Instead, you'll need to use one or more of the following methods (guarded by appropriate
#ifdef
lines):Win32
Linux, Solaris, AIX and Mac OS X >=10.4 (i.e. Tiger onwards)
FreeBSD, MacOS X, NetBSD, OpenBSD, etc.
HPUX
IRIX
Objective-C (Mac OS X >=10.5 or iOS)