您可以通过使用hwloc库的rust绑定来完成此操作,如本博客文章中所述。
它可以在各种平台下使用,并允许您获取物理核心和逻辑处理单元的数量(在超线程的情况下),如下所示(代码来自博客文章):
extern crate hwloc;
use hwloc::{Topology, ObjectType};
fn main() {
// Create a new Topology
let topology = Topology::new();
// Get all objects with type "Core"
let cores = topology.objects_with_type(&ObjectType::Core);
// Match on the returned Result and print the length if successful.
match cores {
Ok(c) => println!("There are {} cores on this machine.", c.len()),
Err(e) => panic!(format!("Could not load cores because of: {:?}", e))
}
}
Run Code Online (Sandbox Code Playgroud)