Function num_cpus::get

source · []
pub fn get() -> usize
Expand description

Returns the number of available CPUs of the current system.

This function will get the number of logical cores. Sometimes this is different from the number of physical cores (See Simultaneous multithreading on Wikipedia).

This will always return at least 1.

Examples

let cpus = num_cpus::get();
if cpus > 1 {
    println!("We are on a multicore system with {} CPUs", cpus);
} else {
    println!("We are on a single core system");
}

Note

This will check sched affinity on Linux, showing a lower number of CPUs if the current thread does not have access to all the computer’s CPUs.

This will also check cgroups, frequently used in containers to constrain CPU usage.