Module workctl::sync_flag [−][src]
SyncFlag
s, or syncronization flags, are one-way Boolean values that can
be shared across threads, sending messages from a single producer to
any number of consumers.
Like std::sync::mpsc
, SyncFlag
s are created in pairs: a transmitter
that can’t be duplicated and a receiver that can be duplicated any number
of times. SyncFlagTx
can be used in, for instance, a controller/main
thread, which can pass clones of the corresponding SyncFlagRx
to the
worker threads it spawns in order to control them.
Examples
use workctl::new_syncflag; use std::thread; // Create a new SyncFlag set to communicate with the spawned thread. let (mut tx, rx) = new_syncflag(true); // This isn't technically needed in this case, but if we were spawning more // than one thread we'd create a clone for each. let thread_rx = rx.clone(); thread::spawn(move || { // Do nothing as long as the sync flag is true. Really, you'd do work here. while thread_rx.get() { thread::yield_now(); } println!("Thread got signal to close."); }); // Do something here, like maybe adding work to a WorkQueue // The program has completed, so set the SyncFlag to false. tx.set(false);
Structs
SyncFlagRx |
|
SyncFlagTx |
|
Functions
new_syncflag | Create a new |