#thread #process #cpu #core #api-bindings

affinity

Provides a consistent way to set core affinity for currently running threads and processes

3 releases

0.1.2 Oct 16, 2021
0.1.1 Apr 3, 2020
0.1.0 Apr 3, 2020

#201 in Operating systems

Download history 2223/week @ 2024-01-08 2515/week @ 2024-01-15 2201/week @ 2024-01-22 2052/week @ 2024-01-29 2299/week @ 2024-02-05 3218/week @ 2024-02-12 2386/week @ 2024-02-19 2463/week @ 2024-02-26 2550/week @ 2024-03-04 2440/week @ 2024-03-11 2629/week @ 2024-03-18 2354/week @ 2024-03-25 2406/week @ 2024-04-01 2294/week @ 2024-04-08 2758/week @ 2024-04-15 2998/week @ 2024-04-22

10,528 downloads per month
Used in 8 crates (3 directly)

MIT/Apache

13KB
211 lines

Affinity

Build Status crates.io mio Lines of Code

This crate provides a consistent way to set core affinity for currently running threads and processes.

Usage

use affinity::*;
fn bind_even_cores() {
    // Select every second core
    let cores: Vec<usize> = (0..get_core_num()).step_by(2).collect();
    println!("Binding thread to cores : {:?}", &cores);
    // Output : "Binding thread to cores : [0, 2, 4, 6]"
    
    set_thread_affinity(&cores).unwrap();
    println!("Current thread affinity : {:?}", get_thread_affinity().unwrap());
    // Output : "Current thread affinity : [0, 2, 4, 6]"
}

Complete example here.

Features

  • Bind to multiple cores
  • Return list of currently bound cores
  • Reliably get number of cores (uses num_cpus)
  • Allow caller to handle errors
  • Supports affinity inheritance for new child processes on Windows (through set_process_affinity())

Platforms

Currently only tested on :

  • Windows
  • Linux (Arch x64)

License

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–7.5MB
~44K SLoC