Skip to main content

set_cpu_affinity

Function set_cpu_affinity 

Source
pub fn set_cpu_affinity(
    thread_id: Option<pid_t>,
    cpus: impl IntoIterator<Item = CpuId>,
) -> Result<()>
Expand description

Set CPU affinity for a thread.

Restricts the thread to run only on the specified CPUs.

§Arguments

  • thread_id - Thread ID to set affinity for. None means the calling thread.
  • cpus - CPU IDs to bind the thread to. Can be any iterable collection.

§Examples

// Pin current thread to CPU 0
set_cpu_affinity(None, [CpuId::new(0)?])?;

// Pin current thread to multiple CPUs
set_cpu_affinity(None, [CpuId::new(0)?, CpuId::new(1)?, CpuId::new(2)?])?;

§Errors

Returns io::ErrorKind::InvalidInput if any CPU ID is too large for cpu_set_t. Returns io::Error if the system call fails, including offline CPUs or CPUs disallowed by the task’s hard cpuset/cgroup.