#thread #task #cancel #stop

stoplight

Stoplight is a simple library for stoppable tasks/threads

5 unstable releases

0.3.3 Oct 30, 2019
0.2.2 Oct 23, 2019
0.2.1 Oct 23, 2019
0.2.0 Oct 23, 2019
0.1.0 Oct 23, 2019

#276 in Concurrency

27 downloads per month

MIT license

5KB
56 lines

Stoplight

Is a small library for stoppable threads/tasks.

use stoplight::Thread;
use std::sync::atomic::{AtomicBool, Ordering};

// spawn our task, this creates a new OS thread.
let th = Thread::spawn(|stop| {
    while !stop.load(Ordering::Relaxed) {}
    42
});

// stop signals the thread to stop.
th.stop();

// join waits for the thread to exit, then gives its return value.
assert_eq!(th.join().unwrap(), 42);

lib.rs:

Stoplight is a small library for stoppable threads/tasks.

 use stoplight;
 use std::sync::atomic::{Ordering};

 // spawn our task, this creates a new OS thread.
 let th = stoplight::spawn(|stop| {
     while !stop.load(Ordering::Relaxed) {}
     42
 });

 // stop() signals the thread to stop, and then join returns its return value.
 th.stop();
 assert_eq!(th.join().unwrap(), 42);

No runtime deps

]