mirror of
https://github.com/guilhermewerner/mini-redis
synced 2025-06-16 06:55:01 +00:00
Implement Some Tokio Tutorials
This commit is contained in:
24
Tutorial/Delay.rs
Normal file
24
Tutorial/Delay.rs
Normal file
@ -0,0 +1,24 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::Notify;
|
||||
|
||||
async fn delay(dur: Duration) {
|
||||
let when = Instant::now() + dur;
|
||||
let notify = Arc::new(Notify::new());
|
||||
let notify2 = notify.clone();
|
||||
|
||||
thread::spawn(move || {
|
||||
let now = Instant::now();
|
||||
|
||||
if now < when {
|
||||
thread::sleep(when - now);
|
||||
}
|
||||
|
||||
notify2.notify_one();
|
||||
});
|
||||
|
||||
notify.notified().await;
|
||||
}
|
Reference in New Issue
Block a user