#thread #priority #schedule #operating-system #pthreads

thread-priority

Library for managing threads priority and schedule policies

29 releases (1 stable)

new 1.0.0 Apr 22, 2024
0.16.0 Feb 28, 2024
0.15.1 Dec 5, 2023
0.15.0 Nov 13, 2023
0.1.0 Jun 22, 2017

#20 in Concurrency

Download history 9388/week @ 2024-01-05 8809/week @ 2024-01-12 10425/week @ 2024-01-19 11313/week @ 2024-01-26 8391/week @ 2024-02-02 10651/week @ 2024-02-09 11961/week @ 2024-02-16 10643/week @ 2024-02-23 10733/week @ 2024-03-01 10211/week @ 2024-03-08 10608/week @ 2024-03-15 9707/week @ 2024-03-22 8556/week @ 2024-03-29 11014/week @ 2024-04-05 11334/week @ 2024-04-12 13834/week @ 2024-04-19

46,276 downloads per month
Used in 30 crates (19 directly)

MIT license

87KB
1K SLoC

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.

Minimal Rust Compiler Version

Is 1.67. If you need any help making it possible to compile with 1.56 please reach out, it should be possible by just downgrading the rstest version to 0.17 or lower (the code is compatible).

Supported platforms

  • Linux
  • Android
  • DragonFly
  • FreeBSD
  • OpenBSD
  • NetBSD
  • macOS
  • iOS
  • Windows

Examples

Minimal cross-platform examples

Setting current thread's priority to minimum:

use thread_priority::*;

fn main() {
    assert!(set_current_thread_priority(ThreadPriority::Min).is_ok());
}

The same as above but using a specific value:

use thread_priority::*;
use std::convert::TryInto;

fn main() {
    // The lower the number the lower the priority.
    assert!(set_current_thread_priority(ThreadPriority::Crossplatform(0.try_into().unwrap())).is_ok());
}

Windows-specific examples

Set the thread priority to the lowest possible value:

use thread_priority::*;

fn main() {
    // The lower the number the lower the priority.
    assert!(set_current_thread_priority(ThreadPriority::Os(WinAPIThreadPriority::Lowest.into())).is_ok());
}

Set the ideal processor for the new thread:

use thread_priority::*;

fn main() {
    std::thread::spawn(|| {
        set_thread_ideal_processor(thread_native_id(), 0);
        println!("Hello world!");
    });
}

Building a thread using the ThreadBuilderExt trait

use thread_priority::*;
use thread_priority::ThreadBuilderExt;

let thread = std::thread::Builder::new()
    .name("MyNewThread".to_owned())
    .spawn_with_priority(ThreadPriority::Max, |result| {
        // This is printed out from within the spawned thread.
        println!("Set priority result: {:?}", result);
        assert!(result.is_ok());
}).unwrap();
thread.join();

Building a thread using the ThreadBuilder.

use thread_priority::*;

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

use thread_priority::*;

assert!(std::thread::current().get_priority().is_ok());
println!("This thread's native id is: {:?}", std::thread::current().get_native_id());

License

This project is licensed under the MIT license.

Dependencies

~135–390KB