当前位置:网站首页>rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
2022-04-23 07:38:00 【聆听--风雨】
Notify配合timeout,在异步编程中实现类似可超时条件变量的效果。代码很简单,如下:
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(); //两秒后通知
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秒后未收到通知而超时
}
});
tokio::spawn(async move {
if let Ok(_) = tokio::time::timeout(Duration::from_secs(3), notify_clone3.notified()).await
{
println!("recive notifyed!"); //等待三秒,但是在第二秒的时候收到了通知,会直接返回成功
}
})
.await
.unwrap();
}
版权声明
本文为[聆听--风雨]所创,转载请带上原文链接,感谢
https://chongfei.blog.csdn.net/article/details/124341012
边栏推荐
- [appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
- js常用数组方法
- Sword finger offer Day24 math (medium)
- An article understands variable lifting
- ATSS(CVPR2020)
- 数论求a^b(a,b为1e12级别)的因子之和
- pdf加水印
- The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
- Online yaml to XML tool
- QFileDialog 选择多个文件或文件夹
猜你喜欢

为什么会存在1px问题?怎么解决?

青苹果影视系统源码 影视聚合 影视导航 影视点播网站源码

Kubernetes in browser and IDE | interactive learning platform killercoda

excle加水印

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

My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together
![[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel](/img/7c/0adc0940b6d5c8a61d34bfa5f66ee7.png)
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel

2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry

There are some problems when using numeric type to query string type fields in MySQL
![[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test](/img/4a/c741ec4f9aa724e150a5ae24d0f9e9.png)
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
随机推荐
Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American
5.6 综合案例-RTU-
一个必看的微信小程序开发指南1-基础知识了解
ELK生产实践
Asan minimalism
常用正则表达式
浅谈ES6尾调优化
Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
刨析——浏览器如何工作
Compiling principle questions - with answers
How to read books and papers
Distributed service governance Nacos
LeetCoed18. Sum of four numbers
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
Online yaml to XML tool
Using qlst excel file
[C语言] 文件操作《一》
sql 使用过的查询语句
校园转转二手市场源码下载
程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法