Skip to main content

set_default_cpu_threads

Function set_default_cpu_threads 

Source
pub fn set_default_cpu_threads(
    threads: NonZeroUsize,
) -> Result<(), AlreadyInitialized>
Expand description

Fix the worker count CpuBackend::new uses for the rest of the process.

The value is read once, by the first CpuBackend::new call that resolves a count; a backend built later keeps whatever policy it was built with. A first CpuBackend::new that cannot build its pool falls back to serial for that backend only and does not fix the default, so a later call tries again. Call this before any other thread can construct a default backend.

§Errors

Returns AlreadyInitialized when the default thread configuration was already fixed successfully, so a change is unsupported: either an earlier call or a CpuBackend::new that resolved a count fixed it. A failed first construction does not fix it.

§Examples

use std::num::NonZeroUsize;
use tensor4all_tensorbackend::{set_default_cpu_threads, AlreadyInitialized};

// Whichever call sets the default first wins, and every later one is
// rejected typed instead of panicking.
let _ = set_default_cpu_threads(NonZeroUsize::new(3).unwrap());
assert_eq!(
    set_default_cpu_threads(NonZeroUsize::MAX),
    Err(AlreadyInitialized)
);