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 policy<VALUE: Into<ThreadSchedulePolicy>>(self, value: VALUE) -> Self
pub fn policy<VALUE: Into<ThreadSchedulePolicy>>(self, value: VALUE) -> Self
The thread’s unix scheduling policy.
For more information, see
crate::unix::ThreadSchedulePolicy
and crate::unix::set_thread_priority_and_policy
.
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 moreSource§impl Debug for ThreadBuilder
impl Debug for ThreadBuilder
Source§impl Default for ThreadBuilder
impl Default for ThreadBuilder
Source§fn default() -> ThreadBuilder
fn default() -> ThreadBuilder
Source§impl Hash for ThreadBuilder
impl Hash for ThreadBuilder
Source§impl Ord for ThreadBuilder
impl Ord for ThreadBuilder
Source§fn cmp(&self, other: &ThreadBuilder) -> Ordering
fn cmp(&self, other: &ThreadBuilder) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for ThreadBuilder
impl PartialEq for ThreadBuilder
Source§impl PartialOrd for ThreadBuilder
impl PartialOrd for ThreadBuilder
impl Eq for ThreadBuilder
impl StructuralPartialEq for ThreadBuilder
Auto Trait Implementations§
impl Freeze for ThreadBuilder
impl RefUnwindSafe for ThreadBuilder
impl Send for ThreadBuilder
impl Sync for ThreadBuilder
impl Unpin for ThreadBuilder
impl UnwindSafe for ThreadBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)