Skip to content

Commit

Permalink
examples: change mutex to channel
Browse files Browse the repository at this point in the history
  • Loading branch information
oneofthezombies committed Feb 12, 2024
1 parent 1a1393d commit 8ac6423
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/examples/readme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition.workspace = true
[dependencies]
kill_tree = { path = "../../libs/kill_tree", features = ["tokio"] }
tokio = { version = "1.36.0", features = ["full"] }
ctrlc = "3.4.2"
ctrlc = { version = "3.4.2", features = ["termination"] }

[[bin]]
name = "kill_tree"
Expand Down
15 changes: 7 additions & 8 deletions crates/examples/readme/src/cleanup_children.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use kill_tree::{blocking::kill_tree_with_config, Config};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::sync::mpsc::channel;

fn cleanup_children() {
let current_process_id = std::process::id();
Expand All @@ -13,16 +12,16 @@ fn cleanup_children() {
}

fn main() {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
let (tx, rx) = channel();

ctrlc::set_handler(move || {
cleanup_children();
r.store(false, Ordering::SeqCst);
tx.send(()).expect("Could not send signal on channel.");
})
.expect("Error setting Ctrl-C handler");
.expect("Error setting handler.");

println!("Waiting for Ctrl-C...");
while running.load(Ordering::SeqCst) {}
println!("Current process id: {}", std::process::id());
println!("Waiting for signal...");
rx.recv().expect("Could not receive from channel.");
println!("Got it! Exiting...");
}

0 comments on commit 8ac6423

Please sign in to comment.