18 releases

Uses old Rust 2015

0.8.3 Mar 1, 2025
0.8.1 Aug 3, 2023
0.8.0 Jan 26, 2023
0.7.6 Dec 1, 2022
0.5.9 Oct 10, 2017

#5 in #cpu-core

Download history 85658/week @ 2026-03-03 453030/week @ 2026-03-10 304505/week @ 2026-03-17 229417/week @ 2026-03-24 243596/week @ 2026-03-31 252757/week @ 2026-04-07 249677/week @ 2026-04-14 267755/week @ 2026-04-21 268141/week @ 2026-04-28 295020/week @ 2026-05-05 319602/week @ 2026-05-12 348056/week @ 2026-05-19 349761/week @ 2026-05-26 301947/week @ 2026-06-02

1,368,394 downloads per month
Used in 693 crates (205 directly)

MIT/Apache

22KB
533 lines

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.

Dependencies

~29–250KB