From 8ac6423920eb8c6019cf9d6e32e8a9b8ac688796 Mon Sep 17 00:00:00 2001 From: oneofthezombies Date: Mon, 12 Feb 2024 23:39:35 +0900 Subject: [PATCH] examples: change mutex to channel --- crates/examples/readme/Cargo.toml | 2 +- crates/examples/readme/src/cleanup_children.rs | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/crates/examples/readme/Cargo.toml b/crates/examples/readme/Cargo.toml index 36b5b3a..f38bfcc 100644 --- a/crates/examples/readme/Cargo.toml +++ b/crates/examples/readme/Cargo.toml @@ -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" diff --git a/crates/examples/readme/src/cleanup_children.rs b/crates/examples/readme/src/cleanup_children.rs index f62f094..aa0ee04 100644 --- a/crates/examples/readme/src/cleanup_children.rs +++ b/crates/examples/readme/src/cleanup_children.rs @@ -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(); @@ -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..."); }