Module workctl::sync_flag[][src]

SyncFlags, 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, SyncFlags 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

SyncFlagRx is the receiving (immutable) half of a Single Producer, Multiple Consumer Boolean (e.g. the opposite of std::sync::mpsc). An number of worker threads can use this to get info from a single controller, for instance.

SyncFlagTx

SyncFlagTx is the transmitting (mutable) half of a Single Producer, Multiple Consumer Boolean (e.g. the opposite of std::sync::mpsc). A single controller can use this to send info to any number of worker threads, for instance.

Functions

new_syncflag

Create a new SyncFlagTx and SyncFlagRx that can be used to share a bool across a number of threads.