Skip to content

Instantly share code, notes, and snippets.

@adililhan
Created October 15, 2022 20:11
Show Gist options
  • Save adililhan/12340ed3a118f20f276b95b09f896359 to your computer and use it in GitHub Desktop.
Save adililhan/12340ed3a118f20f276b95b09f896359 to your computer and use it in GitHub Desktop.
reentrant function in Rust
use nix::sys::signal::*;
use nix::sys::signal::SigAction;
use std::time::Duration;
use std::thread;
fn calculate(first: i32, second: i32) -> i32 {
let sum = first + second;
thread::sleep(Duration::from_secs(3));
sum
}
extern fn signal_handler(_: i32) {
let sum: i32 = 50 + 60;
println!("Interrupt caught!\n");
println!("Result from signal_handler: {}", sum);
println!("---\n");
}
fn main() {
let sa = SigAction::new(SigHandler::Handler(signal_handler),
SaFlags::empty(),
SigSet::empty());
unsafe {
sigaction(SIGINT, &sa).unwrap();
}
loop {
println!("Result from main: {}", calculate(3, 5));
println!("---\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment