当前位置:网站首页>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
边栏推荐
- jsp页面编码
- Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
- Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
- 怎么读书读论文
- Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
- [C语言] 文件操作《一》
- 线程的调度(优先级)
- Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
- vmware 搭建ES8的常见错误
- 对OutputStream类的flush()方法的误解
猜你喜欢

Failed to convert a NumPy array to a Tensor(Unsupported Object type int)

excle加水印

Flink SQL实现流批一体

stm32以及freertos 堆栈解析

synchronized 实现原理

关于ORB——SLAM运行中关键帧位置越来越近的异常说明

Using qlst excel file

Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code

LeetCode中等题之旋转函数

Rearranging log files for leetcode simple question
随机推荐
Generate and parse tokens using JWT
Protobuf简介
Shell脚本进阶
Search the complete navigation program source code
队列(c语言/链表)
Redis master-slave server problem
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
QT compilation qtxlsx Library
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
Online app resource download website source code
CGM优化血糖监测管理——移宇科技亮相四川省国际医学交流促进会
Input / output system
分布式消息中间件框架选型-数字化架构设计(7)
[effective go Chinese translation] part I
LeetCode-199-二叉树的右视图
作文以记之 ~ 二叉树的后序遍历
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
获取TrustedInstaller权限
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解