工作中绑定到指定CPU的核心上性能会大大提升。
以下是绑定方式
配置cargo.toml文件
[dependencies]
core_affinity = "0.5.10"
实现部分
use std::fmt::Debug;
extern crate core_affinity;
use std::thread;
use std::time::Duration;
fn main() {
let core_ids = core_affinity::get_core_ids().unwrap();
let handles: Vec<_> = core_ids.into_iter().enumerate().map(|(i, id)| {
thread::spawn(move || {
// 绑定当前线程到特定的CPU核心
core_affinity::set_for_current(id);
loop {
println!("{:?}",id);
thread::sleep(Duration::from_secs(1));
}
})
}).collect();
// 等待所有线程结束
for handle in handles {
handle.join().unwrap();
}
}
测试结果:
相关链接
发表评论