pub fn init() -> Result<KillWait, Error>Expand description
Register signal handler for SIGINT and SIGTERM.
This function must be called before any other signal management and any threads are launched.
Note that this doesn’t actually set a callback. That must be done using
register().
§Errors
Error::Internal indicates that the internal signal monitoring thread
failed to launch.
§Panics
If the signal mask can not be set this function will panic.
Examples found in repository?
examples/wait.rs (line 7)
3fn main() {
4 //
5 // Initialize signal handler
6 //
7 let kw = simple_sigh::init().unwrap();
8
9 //
10 // Set a signal handler callback
11 //
12 #[allow(clippy::significant_drop_tightening)]
13 simple_sigh::register(move |_| {
14 println!("Received signal");
15 })
16 .unwrap();
17
18 //
19 // Wait until abort
20 //
21 println!("Wait for interrupt/termination signal..");
22 kw.wait();
23 println!("Bye!");
24}More examples
examples/breakout.rs (line 23)
19fn main() {
20 //
21 // Initialize signal handler
22 //
23 let _ = simple_sigh::init();
24
25 //
26 // Prepare shared state
27 //
28 let sh = Shared::default();
29 let sh = Arc::new(sh);
30
31 //
32 // Set a signal handler callback
33 //
34 let sh2 = Arc::clone(&sh);
35 #[allow(clippy::significant_drop_tightening)]
36 simple_sigh::register(move |_| {
37 println!("Received signal");
38 let mut g = sh2.rw.lock();
39 g.term = true;
40 sh2.signal.notify_one();
41 })
42 .unwrap();
43
44 //
45 // Wait until abort
46 //
47 println!("Wait for interrupt/termination signal..");
48 let mut g = sh.rw.lock();
49 while !g.term {
50 sh.signal.wait(&mut g);
51 }
52 println!("Bye!");
53}