[][src]Crate ctrlc

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.


extern crate ctrlc;
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

Handling of SIGTERM can be enabled with termination feature. If this is enabled, the handler specified by set_handler() will be executed for both SIGINT and SIGTERM.



Ctrl-C error.


A cross-platform way to represent Ctrl-C or program termination signal. Other signals/events are supported via Other-variant.



Register signal handler for Ctrl-C.

Type Definitions


Platform specific signal type