https://github.com/elast0ny/affinity
Set process and thread affinity in your Rust programs !
https://github.com/elast0ny/affinity
Last synced: 3 months ago
JSON representation
Set process and thread affinity in your Rust programs !
- Host: GitHub
- URL: https://github.com/elast0ny/affinity
- Owner: elast0ny
- Created: 2020-04-03T15:46:36.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-09-04T23:43:13.000Z (over 2 years ago)
- Last Synced: 2026-01-13T20:58:12.850Z (5 months ago)
- Language: Rust
- Size: 26.4 KB
- Stars: 30
- Watchers: 2
- Forks: 8
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Affinity
[](https://github.com/elast0ny/affinity-rs/actions?query=workflow%3Abuild)
[](https://crates.io/crates/affinity)
[](https://docs.rs/affinity/)
[](https://tokei.rs/b1/github/elast0ny/affinity-rs?category=code)
This crate provides a consistent way to set core affinity for currently running threads and processes.
## Usage
```rust
use affinity::*;
fn bind_even_cores() {
// Select every second core
let cores: Vec = (0..get_core_num()).step_by(2).collect();
println!("Binding thread to cores : {:?}", &cores);
// Output : "Binding thread to cores : [0, 2, 4, 6]"
set_thread_affinity(&cores).unwrap();
println!("Current thread affinity : {:?}", get_thread_affinity().unwrap());
// Output : "Current thread affinity : [0, 2, 4, 6]"
}
```
Complete example [here](https://github.com/elast0ny/affinity-rs/blob/master/examples/main.rs).
## Features
- Bind to multiple cores
- Return list of currently bound cores
- Reliably get number of cores (uses [num_cpus](https://crates.io/crates/num_cpus))
- Allow caller to handle errors
- Supports affinity inheritance for new child processes on Windows (through `set_process_affinity()`)
## Platforms
Currently only tested on :
- Windows
- Linux (Arch x64)
## License
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
* [MIT license](http://opensource.org/licenses/MIT)
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.