Trait thread_priority::ThreadBuilderExt

source ·
pub trait ThreadBuilderExt {
    // Required methods
    fn spawn_with_priority<F, T>(
        self,
        priority: ThreadPriority,
        f: F
    ) -> Result<JoinHandle<T>>
       where F: FnOnce(Result<(), Error>) -> T + Send + 'static,
             T: Send + 'static;
    fn spawn_scoped_with_priority<'scope, 'env, F, T>(
        self,
        scope: &'scope Scope<'scope, 'env>,
        priority: ThreadPriority,
        f: F
    ) -> Result<ScopedJoinHandle<'scope, T>>
       where F: FnOnce(Result<(), Error>) -> T + Send + 'scope,
             T: Send + 'scope;
}
Expand description

Adds thread building functions using the priority.

Required Methods§

source

fn spawn_with_priority<F, T>( self, priority: ThreadPriority, f: F ) -> Result<JoinHandle<T>>
where F: FnOnce(Result<(), Error>) -> T + Send + 'static, T: Send + 'static,

Spawn a thread with set priority. The passed functor f is executed in the spawned thread and receives as the only argument the result of setting the thread priority. See std::thread::Builder::spawn and ThreadPriority::set_for_current for more info.

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

fn spawn_scoped_with_priority<'scope, 'env, F, T>( self, scope: &'scope Scope<'scope, 'env>, priority: ThreadPriority, f: F ) -> Result<ScopedJoinHandle<'scope, T>>
where F: FnOnce(Result<(), Error>) -> T + Send + 'scope, T: Send + 'scope,

Spawn a scoped thread with set priority. The passed functor f is executed in the spawned thread and receives as the only argument the result of setting the thread priority. See std::thread::Builder::spawn_scoped and ThreadPriority::set_for_current for more info.

§Example
use thread_priority::*;
use thread_priority::ThreadBuilderExt;

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl ThreadBuilderExt for Builder

source§

fn spawn_with_priority<F, T>( self, priority: ThreadPriority, f: F ) -> Result<JoinHandle<T>>
where F: FnOnce(Result<(), Error>) -> T + Send + 'static, T: Send + 'static,

source§

fn spawn_scoped_with_priority<'scope, 'env, F, T>( self, scope: &'scope Scope<'scope, 'env>, priority: ThreadPriority, f: F ) -> Result<ScopedJoinHandle<'scope, T>>
where F: FnOnce(Result<(), Error>) -> T + Send + 'scope, T: Send + 'scope,

Implementors§