Expand description
A simple implementation of a kill switch, using only the standard library
§Example
use std::time::Duration;
use killswitch_std::KillSwitch;
// Create a kill switch
let kill = KillSwitch::default();
println!("Is kill switch set? {}", kill);
// Now make a couple of clones and check
let k1 = kill.watcher();
let t1 = tokio::spawn(async move {
let duration = Duration::from_secs(2);
for _ in 0..5 {
tokio::time::sleep(duration).await;
println!("Kill switch on thread 1: {}", k1);
}
println!("Thread 1 wrapping up");
});
// Now make a couple of clones and check
let k2 = kill.watcher();
let t2 = tokio::spawn(async move {
let duration = Duration::from_secs(2);
for _ in 0..5 {
tokio::time::sleep(duration).await;
println!("Kill switch on thread 2: {}", k2);
}
println!("Thread 2 wrapping up");
});
let duration = Duration::from_secs(7);
tokio::time::sleep(duration).await;
println!("Flipping kill switch on main thread");
let _ = kill.kill();
let _ = tokio::join!(t1, t2);
println!("All threads finished");
Structs§
- Convenience type which wraps a
AtomicBool
. Initially,is_alive()
will returntrue
. The value can be cloned across threads, and once it has beenkill()
ed, then all of the clones will returnfalse
fromis_alive()
.