I am writing a rust program that does an action every 2 seconds. The program also uses the ctrlc
crate to handle interrupts. However, in some cases, I have to wait up to 2 secs when killing the process. I am using the default std::thread::sleep
function, which I seemingly cannot interrupt.
Here comes my question. Is it possible for me to make my program sleep in a interruptible way ? In other words, does rust have a default feature for interrupting a sleeping program ? I know that C automatically interrupts any sleep function when the SIGINT signal is received.
Thanks in advance
CodePudding user response:
Your signal handler needs to either make the running threads somehow stop or exit the process upon catching the signal.
Exitting the process is simple: call std::process::exit()
in the signal handler after your cleanup is done.
Forwarding the signal to your sleeping threads is harder, you might be able to do that by setting some flag in an Arc<AtomicBool>
upon catching SIGINT and repeatedly check that flag with shorter intermittent sleeps in between checks instead of a single longer sleep.
You could also come up with a solution based on channels where your ctrlc handler communicates with the running threads through a broadcast channel and waits for them to respond with some kind of done
message before exitting the process. This done
message could be through the sending end of a channel that the shutdown handler passes to the running thread. It could then wait with a timeout before force-quitting with std::process::exit
.