Crate thread_priority
source · [−]Expand description
Thread priority. A library for changing thread’s priority.
Usage
Setting thread priority to minimum:
use thread_priority::*;
assert!(set_current_thread_priority(ThreadPriority::Min).is_ok());
// Or like this:
assert!(ThreadPriority::Min.set_for_current().is_ok());
More examples
Minimal cross-platform examples
Setting current thread’s priority to minimum:
use thread_priority::*;
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;
// The lower the number the lower the priority.
assert!(set_current_thread_priority(ThreadPriority::Crossplatform(0.try_into().unwrap())).is_ok());
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());
Re-exports
pub use unix::*;
Modules
This module defines the unix thread control.
Structs
Represents an OS thread.
A copy of the std::thread::Builder
builder allowing to set priority settings.
Platform-specific thread priority value.
Platform-independent thread priority value.
Should be in [0; 100)
range. The higher the number is - the higher
the priority.
Enums
A error type
Thread priority enumeration.
Traits
Adds thread building functions using the priority.
Functions
Spawns a thread with the specified priority.
Spawns a thread with the specified priority.
This is different from spawn
in a way that the passed function doesn’t
need to accept the ThreadPriority::set_for_current
result.
In case of an error, the error is logged using the logging facilities.