Created
October 15, 2022 20:11
-
-
Save adililhan/12340ed3a118f20f276b95b09f896359 to your computer and use it in GitHub Desktop.
reentrant function in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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