pub struct StopToken { /* private fields */ }Expand description
Read-only handle to a thread’s cooperative-cancellation flag.
Cheap to copy, Send + Sync. Reads the per-thread atomic flag set
by JoinHandle::request_stop (or implicitly by JoinHandle’s
Drop).
Workers spawned via Builder::spawn receive a StopToken as
their entry argument and poll StopToken::is_stopped to break
cleanly out of their loop. Tokens can be cloned and passed to
helper functions that need to bail out without being handed the
owning JoinHandle.
use ove::{Thread, StopToken, Priority};
let h = Thread::builder()
.name(c"worker")
.priority(Priority::Normal)
.stack_size(4096)
.spawn(|tok: StopToken| {
while !tok.is_stopped() {
// do work
}
})?;
// h goes out of scope -> Drop calls request_stop + join.Implementations§
Source§impl StopToken
impl StopToken
Sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Construct an empty token that never signals stop. Useful as a default for fields filled in later.
Sourcepub fn is_stopped(&self) -> bool
pub fn is_stopped(&self) -> bool
true if JoinHandle::request_stop (or the parent’s Drop)
has been called on the referenced thread. Returns false for
an empty token.
Sourcepub fn stop_possible(&self) -> bool
pub fn stop_possible(&self) -> bool
true if this token references a real thread (vs.
StopToken::empty).
Sourcepub fn raw_handle(&self) -> ove_thread_t
pub fn raw_handle(&self) -> ove_thread_t
Raw handle accessor for advanced use.