A MoonBit library for managing CPU core affinities to control thread-to-core binding.
This library enables precise control over CPU core affinity, allowing you to bind threads to specific cores. It's particularly useful for high-performance applications, real-time systems, and scenarios where you need to optimize CPU cache locality, reduce context switching overhead, or isolate workloads on dedicated cores.
Add justjavac/core_affinity
to your dependencies:
moon update
moon add justjavac/core_affinity
fn main {
// Get available core IDs
let core_ids = @core_affinity.get_core_ids()
println("Available core IDs: \{core_ids}")
// Early return if no cores are available
if core_ids.length() == 0 {
println("No available cores found.")
return
}
let first_core_id = core_ids[0]
println("Setting affinity to core: \{first_core_id}")
let success = @core_affinity.set_for_current([first_core_id])
if not(success) {
println("Failed to set affinity to core: \{first_core_id}")
return
}
println("Successfully set affinity to core: \{first_core_id}")
}
Platform | get_core_ids() | set_for_current() | Implementation |
---|---|---|---|
Windows | ✅ | ✅ | Win32 API (GetProcessAffinityMask , SetThreadAffinityMask ) |
Linux | ✅ | ✅ | POSIX (sched_getaffinity , sched_setaffinity ) |
macOS | ✅ | ✅ | BSD/Darwin thread affinity APIs |
Other Unix | Limited support, platform-dependent |
- ✅ = Full support
⚠️ = Limited/platform-dependent support
fn main {
let cores = @core_affinity.get_core_ids()
println("Available cores: \{cores}")
}
fn bind_to_first_core() -> Bool {
let cores = @core_affinity.get_core_ids()
if cores.length() > 0 {
@core_affinity.set_for_current([cores[0]])
} else {
false
}
}
fn bind_to_even_cores() -> Bool {
let cores = @core_affinity.get_core_ids()
let even_cores = []
for core in cores {
if core % 2 == 0 {
even_cores.push(core)
}
}
if even_cores.length() > 0 {
@core_affinity.set_for_current(even_cores)
} else {
false
}
}
You can find example usage in the example/
directory:
moon run --target native -C example .
MIT License - see LICENSE file for details.