pub struct ThreadBuilder { /* private fields */ }
Expand description
A copy of the std::thread::Builder
builder allowing to set priority settings.
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();
If the compiler version is at least 1.63, the scoped thread support is also enabled.
use thread_priority::*;
// 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);
Implementations§
Source§impl ThreadBuilder
impl ThreadBuilder
Sourcepub fn name<VALUE: Into<String>>(self, value: VALUE) -> Self
pub fn name<VALUE: Into<String>>(self, value: VALUE) -> Self
Names the thread-to-be. Currently the name is used for identification only in panic messages.
The name must not contain null bytes (\0
).
For more information about named threads, see
std::thread::Builder::name()
.
Sourcepub fn stack_size<VALUE: Into<usize>>(self, value: VALUE) -> Self
pub fn stack_size<VALUE: Into<usize>>(self, value: VALUE) -> Self
Sets the size of the stack (in bytes) for the new thread.
The actual stack size may be greater than this value if the platform specifies a minimal stack size.
For more information about the stack size for threads, see
std::thread::Builder::stack_size()
.
Sourcepub fn priority<VALUE: Into<ThreadPriority>>(self, value: VALUE) -> Self
pub fn priority<VALUE: Into<ThreadPriority>>(self, value: VALUE) -> Self
The thread’s custom priority.
For more information about the stack size for threads, see
ThreadPriority
.
Sourcepub fn winapi_priority<VALUE: Into<WinAPIThreadPriority>>(
self,
value: VALUE,
) -> Self
pub fn winapi_priority<VALUE: Into<WinAPIThreadPriority>>( self, value: VALUE, ) -> Self
The WinAPI priority representation.
For more information, see
crate::windows::WinAPIThreadPriority
.
Sourcepub fn boost_enabled(self, value: bool) -> Self
pub fn boost_enabled(self, value: bool) -> Self
Disables or enables the ability of the system to temporarily boost the priority of a thread.
For more information, see
crate::windows::set_thread_priority_boost
.
Sourcepub fn ideal_processor<VALUE: Into<IdealProcessor>>(self, value: VALUE) -> Self
pub fn ideal_processor<VALUE: Into<IdealProcessor>>(self, value: VALUE) -> Self
Sets a preferred processor for a thread. The system schedules threads on their preferred processors whenever possible.
For more information, see
crate::windows::set_thread_ideal_processor
.
Sourcepub fn spawn<F, T>(self, f: F) -> Result<JoinHandle<T>>
pub fn spawn<F, T>(self, f: F) -> Result<JoinHandle<T>>
Spawns a new thread by taking ownership of the Builder
, and returns an
std::io::Result
to its std::thread::JoinHandle
.
Sourcepub fn spawn_scoped<'scope, 'env, F, T>(
self,
scope: &'scope Scope<'scope, 'env>,
f: F,
) -> Result<ScopedJoinHandle<'scope, T>>
pub fn spawn_scoped<'scope, 'env, F, T>( self, scope: &'scope Scope<'scope, 'env>, f: F, ) -> Result<ScopedJoinHandle<'scope, T>>
Spawns a new scoped thread by taking ownership of the Builder
, and returns an
std::io::Result
to its std::thread::ScopedJoinHandle
.
Sourcepub fn spawn_careless<F, T>(self, f: F) -> Result<JoinHandle<T>>
pub fn spawn_careless<F, T>(self, f: F) -> Result<JoinHandle<T>>
Spawns a new thread by taking ownership of the Builder
, and returns an
std::io::Result
to its std::thread::JoinHandle
.
Sourcepub fn spawn_scoped_careless<'scope, 'env, F, T>(
self,
scope: &'scope Scope<'scope, 'env>,
f: F,
) -> Result<ScopedJoinHandle<'scope, T>>
pub fn spawn_scoped_careless<'scope, 'env, F, T>( self, scope: &'scope Scope<'scope, 'env>, f: F, ) -> Result<ScopedJoinHandle<'scope, T>>
Spawns a new scoped thread by taking ownership of the Builder
, and returns an
std::io::Result
to its std::thread::ScopedJoinHandle
.
Trait Implementations§
Source§impl Clone for ThreadBuilder
impl Clone for ThreadBuilder
Source§fn clone(&self) -> ThreadBuilder
fn clone(&self) -> ThreadBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more