Skip to content

Commit adb6ee3

Browse files
brsonclaude
andcommitted
Add ctrlc crate documentation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 142a686 commit adb6ee3

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

crates/rustmax/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,7 @@ pub mod comrak {
354354

355355
#[cfg(feature = "ctrlc")]
356356
pub 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
}

src/linksubs.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
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":

0 commit comments

Comments
 (0)