Skip to content

Commit 8ac6423

Browse files
committed
examples: change mutex to channel
1 parent 1a1393d commit 8ac6423

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

crates/examples/readme/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition.workspace = true
66
[dependencies]
77
kill_tree = { path = "../../libs/kill_tree", features = ["tokio"] }
88
tokio = { version = "1.36.0", features = ["full"] }
9-
ctrlc = "3.4.2"
9+
ctrlc = { version = "3.4.2", features = ["termination"] }
1010

1111
[[bin]]
1212
name = "kill_tree"
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use kill_tree::{blocking::kill_tree_with_config, Config};
2-
use std::sync::atomic::{AtomicBool, Ordering};
3-
use std::sync::Arc;
2+
use std::sync::mpsc::channel;
43

54
fn cleanup_children() {
65
let current_process_id = std::process::id();
@@ -13,16 +12,16 @@ fn cleanup_children() {
1312
}
1413

1514
fn main() {
16-
let running = Arc::new(AtomicBool::new(true));
17-
let r = running.clone();
15+
let (tx, rx) = channel();
1816

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

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

0 commit comments

Comments
 (0)