ctrlc 2.0.1

Easy Ctrl-C handler for Rust projects

Crate ctrlc [] [src]

A simple easy to use wrapper around Ctrl-C.


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);
    println!("Waiting for Ctrl-C...");
    while running.load(Ordering::SeqCst) {}
    println!("Got it! Exiting...");



Sets up the signal handler for Ctrl-C using default polling rate of 100ms.


Sets up the signal handler for Ctrl-C using a custom polling rate. The polling rate is the amount of time the internal spinloop of CtrlC sleeps between iterations. Because condition variables are not safe to use inside a signal handler, CtrlC (from version 1.1.0) uses a spinloop and an atomic boolean instead.