#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

#204 in Operating systems

Download history 1741/week @ 2023-12-14 1011/week @ 2023-12-21 1634/week @ 2023-12-28 2543/week @ 2024-01-04 2469/week @ 2024-01-11 2509/week @ 2024-01-18 2016/week @ 2024-01-25 1964/week @ 2024-02-01 2984/week @ 2024-02-08 2621/week @ 2024-02-15 2451/week @ 2024-02-22 2745/week @ 2024-02-29 2237/week @ 2024-03-07 2637/week @ 2024-03-14 2759/week @ 2024-03-21 1487/week @ 2024-03-28

9,659 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–8MB
~45K SLoC