Crate core_affinity

source ·
Expand description

This crate manages CPU affinities.


This example shows how to 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 active CPU cores.
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.

for handle in handles.into_iter() {


  • This represents a CPU core.


  • This function tries to retrieve information on all the “cores” on which the current thread is allowed to run.
  • This function tries to pin the current thread to the specified core.