thread-priority

CI Crates Docs MIT licensed

A simple library to control thread schedule policies and thread priority.

If your operating system isn't yet supported, please, create an issue.

Supported platforms

Examples

Minimal cross-platform examples

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()); } ```

Windows-specific examples

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!"); }); } ```

Building a thread using the ThreadBuilderExt trait

```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(); ```

Building a thread using the ThreadBuilder.

```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(); ```

Using ThreadExt trait on the current thread

```rust,norun use threadpriority::*;

assert!(std::thread::current().getpriority().isok()); println!("This thread's native id is: {:?}", std::thread::current().getnativeid()); ```

License

This project is licensed under the MIT license.