Rust crate for managing CPU core affinities
Find a file
Jason Volk 7c7a9dea35
add set_each_for_current()
run rustfmt

Signed-off-by: Jason Volk <jason@zemos.net>
2026-03-27 18:15:48 +00:00
src add set_each_for_current() 2026-03-27 18:15:48 +00:00
.appveyor.yml Added more build platforms and improved documentation 2017-10-10 09:47:09 -04:00
.gitignore Initial commit 2017-09-14 11:48:35 -04:00
.travis.yml Added more build platforms and improved documentation 2017-10-10 09:47:09 -04:00
Cargo.toml Merge pull request #25 from paolobarbolini/spdx 2025-03-01 14:34:13 -05:00
LICENSE-APACHE Added documentation 2017-09-15 10:20:21 -04:00
LICENSE-MIT Added documentation 2017-09-15 10:20:21 -04:00
README.md Added message about NetBSD support 2025-03-01 14:32:00 -05:00

core_affinity_rs is a Rust crate for managing CPU affinities. It currently supports Linux, Mac OSX, and Windows.

Documentation

Example

This example shows how create a thread for each available processor and pin each thread to its corresponding processor.

extern crate core_affinity;

use std::thread;

// Retrieve the IDs of all cores on which the current
// thread is allowed to run.
// NOTE: If you want ALL the possible cores, you should
// use num_cpus.
let core_ids = core_affinity::get_core_ids().unwrap();

// Create a thread for each active CPU core.
let handles = core_ids.into_iter().map(|id| {
    thread::spawn(move || {
        // Pin this thread to a single CPU core.
        let res = core_affinity::set_for_current(id);
        if (res) {
          // Do more work after this.
        }
    })
}).collect::<Vec<_>>();

for handle in handles.into_iter() {
    handle.join().unwrap();
}

Platforms

core_affinity_rs should work on Linux, Windows, Mac OSX, FreeBSD, NetBSD, and Android.