sigterm 0.3.10

Signal-aware async control and cancellation primitives for Tokio.
Documentation
/* examples/broadcast.rs */

//! Example of using `Broadcast` for multiple tasks.

use sigterm::Broadcast;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
	let broadcast = Broadcast::new();

	// Spawn multiple worker tasks
	for i in 1..=3 {
		let sub = broadcast.subscribe();
		tokio::spawn(async move {
			println!("Worker {} started...", i);
			sub.recv().await;
			println!("Worker {} shutting down!", i);
		});
	}

	// Simulate work
	sleep(Duration::from_secs(2)).await;

	println!("Triggering global shutdown...");
	broadcast.shutdown();

	// Give tasks time to print their shutdown message
	sleep(Duration::from_millis(100)).await;
	println!("Main exited.");
}