A simple library to control thread schedule policies and thread priority.
If your operating system isn't yet supported, please, create an issue.
Is 1.46
. If you need any help making it possible to compile with 1.36
please reach out.
Setting current thread's priority to minimum:
```rust,norun use threadpriority::*;
fn main() { assert!(setcurrentthreadpriority(ThreadPriority::Min).isok()); } ```
The same as above but using a specific value:
```rust,norun use threadpriority::*; use std::convert::TryInto;
fn main() { // The lower the number the lower the priority. assert!(setcurrentthreadpriority(ThreadPriority::Crossplatform(0.tryinto().unwrap())).is_ok()); } ```
Set the thread priority to the lowest possible value:
```rust,norun use threadpriority::*;
fn main() { // The lower the number the lower the priority. assert!(setcurrentthreadpriority(ThreadPriority::Os(WinAPIThreadPriority::Lowest.into())).isok()); } ```
Set the ideal processor for the new thread:
```rust,norun use threadpriority::*;
fn main() { std::thread::spawn(|| { setthreadidealprocessor(threadnative_id(), 0); println!("Hello world!"); }); } ```
```rust,norun use threadpriority::*; use thread_priority::ThreadBuilderExt;
let thread = std::thread::Builder::new() .name("MyNewThread".toowned()) .spawnwithpriority(ThreadPriority::Max, |result| { // This is printed out from within the spawned thread. println!("Set priority result: {:?}", result); assert!(result.isok()); }).unwrap(); thread.join(); ```
```rust,norun use threadpriority::*;
let thread = ThreadBuilder::default() .name("MyThread") .priority(ThreadPriority::Max) .spawn(|result| { // This is printed out from within the spawned thread. println!("Set priority result: {:?}", result); assert!(result.is_ok()); }).unwrap(); thread.join();
// Another example where we don't care about the priority having been set. let thread = ThreadBuilder::default() .name("MyThread") .priority(ThreadPriority::Max) .spawn_careless(|| { // This is printed out from within the spawned thread. println!("We don't care about the priority result."); }).unwrap(); thread.join(); ```
```rust,norun use threadpriority::*;
assert!(std::thread::current().getpriority().isok()); println!("This thread's native id is: {:?}", std::thread::current().getnativeid()); ```
This project is licensed under the MIT license.