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

#25 in Operating systems

Download history 168108/week @ 2024-11-21 101173/week @ 2024-11-28 173288/week @ 2024-12-05 184223/week @ 2024-12-12 82241/week @ 2024-12-19 48610/week @ 2024-12-26 162442/week @ 2025-01-02 200632/week @ 2025-01-09 150908/week @ 2025-01-16 176435/week @ 2025-01-23 181351/week @ 2025-01-30 206138/week @ 2025-02-06 183130/week @ 2025-02-13 218196/week @ 2025-02-20 224052/week @ 2025-02-27 194865/week @ 2025-03-06

864,635 downloads per month
Used in 267 crates (101 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

~28–255KB