Function notify

Source
pub fn notify(signals: &[Signal]) -> Receiver<Signal>
Expand description

Create a new channel subscribed to the given signals.

The channel returned is never closed.

This is a convenience function for subscribing to multiple signals at once. See the documentation of notify_on for details.

The channel returned has a small buffer to prevent signals from being dropped.

THIS MUST BE CALLED BEFORE ANY OTHER THREADS ARE SPAWNED IN YOUR PROCESS.

ยงExample

use chan_signal::Signal;

let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]);

// Blocks until this process is sent an INT or TERM signal.
// Since the channel is never closed, we can unwrap the received value.
signal.recv().unwrap();
Examples found in repository?
examples/simple.rs (line 11)
10fn main() {
11    let signal = notify(&[Signal::INT]);
12    println!("Send a INT signal my way!");
13    // block until we get a signal
14    assert_eq!(signal.recv(), Some(Signal::INT));
15    println!("Thanks :]");
16}
More examples
Hide additional examples
examples/sleep.rs (line 10)
9fn main() {
10    let signal = notify(&[Signal::INT]);
11    println!("Send a INT signal my way!");
12    thread::spawn(move || thread::sleep(Duration::from_secs(10)));
13    // block until we get a signal
14    assert_eq!(signal.recv(), Some(Signal::INT));
15    println!("Thanks :]");
16}
examples/test_block_specific.rs (line 8)
7fn main() {
8    let r_usr1 = chan_signal::notify(&[Signal::USR1, Signal::ALRM]);
9    kill_this(Signal::USR1);
10    kill_this(Signal::ALRM);
11    assert_eq!(r_usr1.recv(), Some(Signal::USR1));
12    assert_eq!(r_usr1.recv(), Some(Signal::ALRM));
13
14    let (s, r_usr2) = chan::sync(1);
15    chan_signal::notify_on(&s, Signal::USR2);
16    kill_this(Signal::USR2);
17    assert_eq!(r_usr2.recv(), Some(Signal::USR2));
18
19    // The following will terminate the process, as it is NOT blocked
20    // by the main thread.
21    kill_this(Signal::TERM);
22    unreachable!();
23}
examples/select.rs (line 12)
10fn main() {
11    // Signal gets a value when the OS sent a INT or TERM signal.
12    let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]);
13    // When our work is complete, send a sentinel value on `sdone`.
14    let (sdone, rdone) = chan::sync(0);
15    // Run work.
16    thread::spawn(move || run(sdone));
17
18    // Wait for a signal or for work to be done.
19    chan_select! {
20        signal.recv() -> signal => {
21            println!("received signal: {:?}", signal)
22        },
23        rdone.recv() => {
24            println!("Program completed normally.");
25        }
26    }
27}
examples/read_names.rs (line 29)
22fn main() {
23    // It is imperative that we start listening for signals as soon as
24    // possible. In particular, if `notify` is called after another thread
25    // has spawned, then signal masking won't be applied to it and signal
26    // handling won't work.
27    //
28    // See "Signal mask and pending signals" section of signal(7).
29    let signal = notify(&[Signal::INT, Signal::TERM]);
30    match run(signal) {
31        Ok(mut names) => {
32            names.sort();
33            println!("You entered {} names: {}",
34                     names.len(), names.connect(", "));
35        }
36        Err(err) => {
37            writeln!(&mut io::stderr(), "{}", err).unwrap();
38            process::exit(1);
39        }
40    }
41}