Crates.io | signalfut |
lib.rs | signalfut |
version | 0.1.0 |
source | src |
created_at | 2024-06-05 07:49:31.097755 |
updated_at | 2024-06-05 07:49:31.097755 |
description | A future similar to tokio::signal::unix::Signal, but can be used with all the runtimes |
homepage | |
repository | https://github.com/SteveLauC/signalfut |
max_upload_size | |
id | 1262524 |
size | 34,960 |
A future similar to tokio::signal::unix::Signal, but can be used with:
Currently, only Linux and macOS are supported. Windows will also be supported in the future.
Greet when receive either SIGINT
or SIGQUIT
:
use signalfut::ctrl_c;
use signalfut::Signal;
use signalfut::SignalFut;
#[tokio::main]
async fn main() {
tokio::select! {
_ = ctrl_c() => {},
_ = SignalFut::new(Signal::SIGQUIT) => {},
}
println!("Greeting!");
}
Let multiple tasks wait for the same signal:
use signalfut::ctrl_c;
#[tokio::main]
async fn main() {
let fut1 = ctrl_c();
let fut2 = ctrl_c();
tokio::join!(fut1, fut2);
}
This crate disposes a signal handler for the signals you want to watch, since signal handler is shared by the whole process, don't use other crates that also do this, they will clash.
After disposition, the default signal handler will not be reset.
This crate creates a helper thread to execute the code that is not signal-safe,
the signal handler and the helper thread communicate through an OS pipe, the only
thing that the signal handler does is write(2)
, which is signal-safe.