Avatar ·

How to programmatically determine the number of CPU cores in C++ (cross-platform solution)?

Hello everyone! I'm writing a multithreaded C++ application for image processing. I want the program to automatically determine the optimal number of threads based on the user's hardware. The problem is that I need it to work on both Windows and Linux.\n\nI tried using macros and system API calls, but it turned into a mess:\n\n```cpp\n#ifdef _WIN32\n SYSTEM_INFO sysinfo;\n GetSystemInfo(&sysinfo);\n int cores = sysinfo.dwNumberOfProcessors;\n#elif __linux__\n int cores = sysconf(_SC_NPROCESSORS_ONLN);\n#else\n int cores = 1; // fallback\n#endif\n```\n\nThis works, but the code looks terrible. Plus, I've read that on some processors with Hyper-Threading, `dwNumberOfProcessors` returns logical cores, not physical ones. But I need exactly the number of physical cores to