Implement Some Tokio Tutorials

This commit is contained in:
GuilhermeWerner
2021-01-19 16:03:55 -03:00
parent 2e7b35e32d
commit 055be51002
12 changed files with 415 additions and 0 deletions

24
Tutorial/Delay.rs Normal file
View 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;
}