#thread #stopped #stop #interface #was #passed #stoppable

stoppable_thread

A wrapper for thread that allows it to be easily stopped cooperatively

7 releases

Uses old Rust 2015

0.2.1 May 6, 2016
0.2.0 May 6, 2016
0.1.4 Mar 12, 2016

#612 in Concurrency

Download history 662/week @ 2023-11-20 664/week @ 2023-11-27 443/week @ 2023-12-04 298/week @ 2023-12-11 263/week @ 2023-12-18 123/week @ 2023-12-25 181/week @ 2024-01-01 387/week @ 2024-01-08 639/week @ 2024-01-15 827/week @ 2024-01-22 412/week @ 2024-01-29 407/week @ 2024-02-05 403/week @ 2024-02-12 321/week @ 2024-02-19 235/week @ 2024-02-26 266/week @ 2024-03-04

1,235 downloads per month
Used in 5 crates (4 directly)

MIT license

7KB
138 lines

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 have gracefully, i.e. by requesting the child thread to stop, partial values can be returned if needed.

Example:

use stoppable_thread;

let handle = stoppable_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();

No runtime deps