num_cpus

    Get the number of CPU cores available on the system.

    cpu
    cpus
    core
    processor
    hardware
    system
    Download zip
    Author
    Version
    0.1.10
    License
    MIT
    Last updated
    8 days ago
    Downloads
    46

    #moonbit-num-cpus

    fn main {
    let logical = @num_cpus.get()
    let physical = @num_cpus.get_physical()
    println("logical: \{logical}, physical: \{physical}")
    }

    get

    fn get() -> Int

    Get the number of CPU cores available on the system.

    This function returns the number of logical CPU cores, which includes hyperthreading cores if available. For example, on a quad-core processor with hyperthreading, this would return 8.

    The function uses FFI (Foreign Function Interface) to call native system APIs to retrieve the CPU count information.

    Returns:
    • The number of logical CPU cores available on the system.

    Example:
    let logical_cores = @num_cpus.get()
    println("Logical CPU cores: \{logical_cores}")

    Note:
    • On multi-threaded systems, this includes all logical cores
    • The result is cached by the operating system and reflects the current hardware configuration
    • This function is thread-safe

    get_physical

    fn get_physical() -> Int

    Get the number of physical CPU cores on the system.

    This function returns the number of physical CPU cores, excluding hyperthreading. For example, on a quad-core processor with hyperthreading, this would return 4 (the actual physical cores), not 8.

    The function uses FFI to call native system APIs that can distinguish between physical and logical cores.

    Returns:
    • The number of physical CPU cores (not including hyperthreading).

    Example:
    let physical_cores = @num_cpus.get_physical()
    let logical_cores = @num_cpus.get()
    println("Physical cores: \{physical_cores}")
    println("Logical cores: \{logical_cores}")
    println("Hyperthreading ratio: \{logical_cores / physical_cores}")

    Note:
    • This count excludes logical cores created by hyperthreading
    • On systems without hyperthreading, this will return the same value as get()
    • The implementation may fall back to logical core count on some platforms
    • This function is thread-safe

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io