Processor
Check available parallelism
Queries the amount of parallelism available to the current process with
std::thread::available_parallelism. This returns the parallelism the program
can actually use: it honors CPU affinity masks and container limits such as
Linux cgroup quotas. The result is a NonZeroUsize, so it is guaranteed to be
at least 1, and it is wrapped in an io::Result because the value cannot
always be determined. This has been available in the standard library since Rust
1.59, making it a good default for sizing thread pools.
use std::thread::available_parallelism;
fn main() -> std::io::Result<()> {
let count = available_parallelism()?.get();
println!("Number of available threads is {}", count);
Ok(())
}
Check number of logical cpu cores
Shows the number of logical CPU cores in current machine using num_cpus::get.
This differs from std::thread::available_parallelism above: num_cpus::get
reports the total number of logical cores on the machine, whereas
available_parallelism reports only the parallelism available to the current
process, accounting for CPU affinity masks and container limits such as Linux
cgroup quotas. num_cpus::get also returns a plain usize rather than a
Result, so it needs no error handling, and num_cpus::get_physical
additionally exposes the count of physical cores, which the standard library
does not provide.
fn main() {
println!("Number of logical cores is {}", num_cpus::get());
}