当前位置:网站首页>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
边栏推荐
- Comparison of indoor positioning technology
- WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
- JS common array methods
- form表单 post提交 数据量大的问题
- Flink SQL实现流批一体
- MySQL数据库中delete、truncate、drop原理详解
- Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
- 获取TrustedInstaller权限
- 关于ORB——SLAM运行中关键帧位置越来越近的异常说明
- 对OutputStream类的flush()方法的误解
猜你喜欢

A simple theme of Typecho with beautiful appearance_ Scarfskin source code download

欧圣电气深交所上市:市值52亿 陆为东父女为美国籍

CGM优化血糖监测管理——移宇科技亮相四川省国际医学交流促进会

SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior

QT reading and writing XML files

Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American

396. Rotate Function

Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9

LeetCode简单题之重新排列日志文件

RPC过程
随机推荐
校园转转二手市场源码下载
耳穴诊疗随笔0421
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
一键清理项目下pycharm和Jupyter缓存文件
5.6 comprehensive case - RTU-
synchronized 实现原理
谈谈那些基础但不简单的股票数据
Flink SQL实现流批一体
Comparison of indoor positioning technology
ASAN 极简原理
Goland 调试go使用-大白记录
欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
让地球少些“碳”息 度能在路上
JS converts tree structure data into one-dimensional array data
对li类数组对象随机添加特性,并进行排序
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
Idea: export Yapi interface using easyyapi plug-in
ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建
Redis master-slave server problem
Overview of bus structure