#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

#196 in Operating systems

Download history 2637/week @ 2024-03-14 2759/week @ 2024-03-21 1953/week @ 2024-03-28 2586/week @ 2024-04-04 2486/week @ 2024-04-11 2995/week @ 2024-04-18 2561/week @ 2024-04-25 2114/week @ 2024-05-02 2357/week @ 2024-05-09 2300/week @ 2024-05-16 3002/week @ 2024-05-23 2401/week @ 2024-05-30 2499/week @ 2024-06-06 2728/week @ 2024-06-13 2706/week @ 2024-06-20 2260/week @ 2024-06-27

10,785 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–7MB
~41K SLoC