You could use OpenMP's omp_get_max_threads ()
function. For g++, enable OpenMP with the -fopenmp directive. For MS Visual Studio enable it in project properties - configuration properties - C/C++ - language. Be aware that omp_get_max_threads ()
may return less than the actual number of hardware cores/threads if omp_set_num_threads ()
has been used to limit the number of threads an OpenMP parallel region is allowed to use (not an issue at program start). You should also be aware that for hyper threading CPUs omp_get_max_threads ()
doesn't return the number of hardware CPU cores, but of hardware threads the CPU supports (and the additional threads provided by hyper threading don't quite cut it).