Skip to main content

Crate thread_priority

Crate thread_priority 

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

// This also support scoped thread.
let x = 0;
std::thread::scope(|s|{
    std::thread::Builder::new()
        .name("MyNewThread".to_owned())
        .spawn_scoped_with_priority(s, ThreadPriority::Max, |result| {
            // This is printed out from within the spawned thread.
            println!("Set priority result: {:?}", result);
            assert!(result.is_ok());
            dbg!(&x);
    }).unwrap();
});

§Building a thread using the ThreadScopeExt trait

use thread_priority::*;
let x = 0;
std::thread::scope(|s|{
    s.spawn_with_priority(ThreadPriority::Max, |result| {
            // This is printed out from within the spawned thread.
            println!("Set priority result: {:?}", result);
            assert!(result.is_ok());
            dbg!(&x);
    });
});

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

// Scoped thread is also supported if the compiler version is at least 1.63.
let mut x = 0;
std::thread::scope(|s|{
    let thread = ThreadBuilder::default()
        .name("MyThread")
        .priority(ThreadPriority::Max)
        .spawn_scoped(s, |result| {
            // This is printed out from within the spawned thread.
            println!("Set priority result: {:?}", result);
            assert!(result.is_ok());
            x += 1;
    }).unwrap();
    thread.join();
});
assert_eq!(x, 1);

// Scoped thread also has a "careless" mode.
std::thread::scope(|s|{
    let thread = ThreadBuilder::default()
        .name("MyThread")
        .priority(ThreadPriority::Max)
        .spawn_scoped_careless(s, || {
            // This is printed out from within the spawned thread.
            println!("We don't care about the priority result.");
            x += 1;
    }).unwrap();
    thread.join();
});
assert_eq!(x, 2);

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

Modules§

unixLinux or macOS or iOS or DragonFly BSD or FreeBSD or OpenBSD or target_os=vxworks or NetBSD or Android or WebAssembly
This module defines the unix thread control.

Structs§

DeadlineFlags
Flags for controlling Deadline scheduling behavior.
SchedAttrLinux or Android
A copy of the Linux kernel’s sched_attr type.
ScheduleParams
Proxy structure to maintain compatibility between glibc and musl
Thread
Represents an OS thread.
ThreadBuilder
A copy of the std::thread::Builder builder allowing to set priority settings.
ThreadPriorityOsValue
Platform-specific thread priority value.
ThreadPriorityValue
Platform-independent thread priority value. Should be in [0; 100) range. The higher the number is - the higher the priority.

Enums§

Error
A error type
NormalThreadSchedulePolicy
Normal (non-realtime) schedule policies For these schedule policies, niceness is used.
PriorityPolicyEdgeValueType
Defines the type of the priority edge value: minimum or maximum.
RealtimeThreadSchedulePolicy
The following “real-time” policies are also supported, for special time-critical applications that need precise control over the way in which runnable processes are selected for execution
ThreadPriority
Thread priority enumeration.
ThreadSchedulePolicy
Thread schedule policy definition.

Constants§

NICENESS_MAX
The maximum value possible for niceness. Threads with this value of niceness have the highest priority possible
NICENESS_MIN
The minimum value possible for niceness. Threads with this value of niceness have the lowest priority possible.

Traits§

ThreadBuilderExt
Adds thread building functions using the priority.
ThreadExt
A helper trait for other threads to implement to be able to call methods on threads themselves.
ThreadScopeExt
Adds scoped thread building functions using the priority.

Functions§

get_current_thread_priority
Get current thread’s priority value.
get_thread_priority
Get the thread’s priority value.
get_thread_scheduling_attributesLinux or Android
Returns scheduling attributes for the current thread.
set_current_thread_priority
Set current thread’s priority. In order to properly map a value of the thread priority, the thread scheduling must be known. This function attempts to retrieve the current thread’s scheduling policy and thus map the priority value correctly, so that it fits within the scheduling policy’s allowed range of values.
set_thread_priority_and_policy
Sets thread’s priority and schedule policy
spawn
Spawns a thread with the specified priority.
spawn_careless
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.
spawn_scoped
Spawns a scoped thread with the specified priority.
spawn_scoped_careless
Spawns a scoped thread with the specified priority. This is different from spawn_scoped 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.
thread_native_id
Returns current thread id, which is the current OS’s native handle. It may or may not be equal or even related to rust’s thread id, there is absolutely no guarantee for that.
thread_schedule_policy
Returns policy parameters (schedule policy and other schedule parameters) for current process
thread_schedule_policy_param
Returns policy parameters (schedule policy and other schedule parameters)

Type Aliases§

ThreadId
An alias type for a thread id.