killable_thread 0.2.2

A wrapper for thread that allows it to be easily stopped cooperatively
Documentation
  • Coverage
  • 66.67%
    10 out of 15 items documented1 out of 15 items with examples
  • Size
  • Source code size: 12.61 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.94 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • normano/stoppable_thread-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • normano

A stoppable, thin wrapper around std::Thread.

Uses std::sync::atomic::AtomicBool and std::thread to create stoppable threads.

The interface is very similar to that of std::thread::Thread (or rather std::thread::JoinHandle) except that every closure passed in must accept a stopped parameter, allowing to check whether or not a stop was requested.

Since all stops must happen gracefully, i.e. by requesting the child thread to stop, partial values can be returned if needed.

Example:

use killable_thread;

let handle = killable_thread::spawn(|stopped| {
    let mut count: u64 = 0;

    while !stopped.get() {
        count += 1
    }

    count
});

// work in main thread

// stop the thread. we also want to collect partial results
let child_count = handle.stop().join().unwrap();