当前位置:网站首页>Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
2022-04-23 08:24:00 【Listen to the wind and rain】
Notify coordination timeout, In asynchronous programming, it can achieve the effect similar to timeout condition variable . The code is simple , as follows :
use std::{sync::Arc, time::Duration};
use tokio::{sync::Notify, time::sleep};
#[tokio::main]
async fn main() {
let notify = Arc::new(Notify::new());
let notify_clone = notify.clone();
let notify_clone2 = notify.clone();
let notify_clone3 = notify.clone();
tokio::spawn(async move {
sleep(Duration::from_secs(2)).await;
notify_clone.notify_one(); // Notify in two seconds
println!("notify one")
});
tokio::spawn(async move {
if let Err(_) = tokio::time::timeout(Duration::from_secs(1), notify_clone2.notified()).await
{
println!("time out!"); //1 Timeout without notification after seconds
}
});
tokio::spawn(async move {
if let Ok(_) = tokio::time::timeout(Duration::from_secs(3), notify_clone3.notified()).await
{
println!("recive notifyed!"); // Wait three seconds , But in the second second second, I received the notice , Will directly return success
}
})
.await
.unwrap();
}
版权声明
本文为[Listen to the wind and rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230738446929.html
边栏推荐
- 跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
- LeetCode-199-二叉树的右视图
- CSV column extract column extraction
- 有意思的js 代码
- 二维01背包
- The third divisor of leetcode simple question
- idea:使用easyYapi插件导出yapi接口
- Using qlst excel file
- Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
- form中enctype属性
猜你喜欢
excle加水印
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
Let the earth have less "carbon" and rest on the road
LeetCode简单题之统计字符串中的元音子字符串
LeetCode简单题之重新排列日志文件
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
[C语言] 文件操作《一》
随机推荐
npm安装yarn
Protobuf简介
freertos学习02-队列 stream buffer message buffer
AAAI 2022 recruit speakers!!
一个必看的微信小程序开发指南1-基础知识了解
单点登录 SSO
Introduction to protobuf
分组背包呀
【深度好文】Flink SQL流批⼀体化技术详解(一)
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
ajax防止缓存方法
基于TCP/IP协议的网络通信实例——文件传输
An example of network communication based on TCP / IP protocol -- file transmission
form中enctype属性
Knowledge points and problem solutions related to information collection
谈谈那些基础但不简单的股票数据
How to encrypt devices under the interconnection of all things
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
二维01背包
ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建