Expand description
Cross platform handling of Ctrl-C signals.
set_handler() allows setting a handler closure which is executed on
Ctrl+C
. On Unix, this corresponds to a SIGINT
signal. On windows, Ctrl+C
corresponds to
CTRL_C_EVENT
or CTRL_BREAK_EVENT
.
Setting a handler will start a new dedicated signal handling thread where we
execute the handler each time we receive a Ctrl+C
signal. There can only be
one handler, you would typically set one at the start of your program.
§Example
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
fn main() {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
}).expect("Error setting Ctrl-C handler");
println!("Waiting for Ctrl-C...");
while running.load(Ordering::SeqCst) {}
println!("Got it! Exiting...");
}
§Handling SIGTERM and SIGHUP
Handling of SIGTERM and SIGHUP
can be enabled with termination
feature. If this is enabled,
the handler specified by set_handler()
will be executed for SIGINT
, SIGTERM
and SIGHUP
.
Enums§
- Ctrl-C error.
- A cross-platform way to represent Ctrl-C or program termination signal. Other signals/events are supported via
Other
-variant.
Functions§
- Register signal handler for Ctrl-C.
- The same as ctrlc::set_handler but errors if a handler already exists for the signal(s).
Type Aliases§
- Platform specific signal type