File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ Simple handling of Ctrl+C for CLI programs.
2+
3+ - Crate [ ` ::ctrlc ` ] .
4+ - [ docs.rs] ( https://docs.rs/ctrlc )
5+ - [ crates.io] ( https://crates.io/crates/ctrlc )
6+ - [ GitHub] ( https://github.com/Detegr/rust-ctrlc )
7+
8+ ---
9+
10+ ` ctrlc ` provides a simple, cross-platform way to set a handler
11+ for the Ctrl+C signal (` SIGINT ` on Unix, ` CTRL_C_EVENT ` on Windows).
12+ It spawns a dedicated signal-handling thread
13+ and invokes a user-provided closure when the signal is received.
14+
15+ The typical pattern is to pair a handler with an [ ` AtomicBool ` ]
16+ that the main loop checks to know when to shut down gracefully.
17+
18+ ## Examples
19+
20+ Graceful shutdown with an atomic flag:
21+
22+ ``` rust,no_run
23+ use std::sync::Arc;
24+ use std::sync::atomic::{AtomicBool, Ordering};
25+
26+ let running = Arc::new(AtomicBool::new(true));
27+ let r = running.clone();
28+
29+ ctrlc::set_handler(move || {
30+ r.store(false, Ordering::SeqCst);
31+ }).expect("Error setting Ctrl-C handler");
32+
33+ while running.load(Ordering::SeqCst) {
34+ // Do work...
35+ break; // (break immediately for doctest)
36+ }
37+
38+ println!("Shutting down.");
39+ ```
40+
41+ [ `AtomicBool` ] : std::sync::atomic::AtomicBool
Original file line number Diff line number Diff line change @@ -354,9 +354,7 @@ pub mod comrak {
354354
355355#[ cfg( feature = "ctrlc" ) ]
356356pub mod ctrlc {
357- //! Simple handling of CTRL-C for CLI programs.
358- //!
359- //! See crate [`::ctrlc`].
357+ #![ doc = include_str ! ( "../doc-src/crate-ctrlc.md" ) ]
360358
361359 pub use :: ctrlc:: * ;
362360}
Original file line number Diff line number Diff line change 104104 "api/crossbeam_utils/struct.Backoff.html" ,
105105 "crate::std::sync" :
106106 "api/std/sync/index.html" ,
107+ "std::sync::atomic::AtomicBool" :
108+ "api/std/sync/atomic/struct.AtomicBool.html" ,
107109 "crate::std::thread" :
108110 "api/std/thread/index.html" ,
109111 "crate::std::sync::mpsc" :
You can’t perform that action at this time.
0 commit comments