[][src]Crate affinity

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]"
}

See

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)

Functions

get_core_num

Returns the number of available cores

get_process_affinity

Returns a list of cores that the current process is bound to

get_thread_affinity

Returns a list of cores that the current thread is bound to

set_process_affinity

Binds the current process to the specified core(s)

set_thread_affinity

Binds the current thread to the specified core(s)