use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{sleep,Duration};
asyncfnperson(remote_arc: Arc<Mutex<i32>>, name: String, new_channel: i32){
// request access to the remoteletmut real_remote = remote_arc.lock().await;
// Chaning the tv channel
*real_remote = new_channel;
println!("{} changed the channel", name);
println!("Watching channel: {}", new_channel);
sleep(Duration::from_secs(5)).await;
}
#[tokio::main]asyncfnmain(){
lettv_channel = 10;
letremote = Mutex::new(tv_channel);
letremote_arc = Arc::new(remote);
letmut task_handles = Vec::new();
for (name,new_channel) in [("Marcus",11),("Jovanna",32),("Carmen",43)]{
task_handles.push(tokio::spawn(person(remote_arc.clone(), name.to_string(), new_channel)));
}
forhandlein task_handles{
handle.await.unwrap();
}
}
Marcus changed the channel
Watching channel: 11
Jovanna changed the channel
Watching channel: 32
Carmen changed the channel
Watching channel: 43
Semaphore
use std::sync::Arc;
use tokio::sync::Semaphore;
use tokio::time::{sleep,Duration};
asyncfnperson(semaphore: Arc<Semaphore>, name: String){
println!("{} is waiting in line", name);
teller(semaphore, name).await;
}
asyncfnteller(semaphore: Arc<Semaphore>, customer: String){
letpermit = semaphore.acquire().await.unwrap();
sleep(Duration::from_secs(2)).await;
println!("\n{} is being served by the teller",customer);
sleep(Duration::from_secs(5)).await;
println!("{} is now leaving the teller",customer);
drop(permit)
}
#[tokio::main]asyncfnmain(){
letnum_of_tellers = 4;
letsemaphore = Semaphore::new(num_of_tellers);
letsemaphore_arc = Arc::new(semaphore);
letmut people_handles = Vec::new();
fornumin0..10{
people_handles.push(
tokio::spawn(person(semaphore_arc.clone(), num.to_string()))
);
}
forhandlein people_handles{
handle.await.unwrap();
}
}
0 is waiting in line
1 is waiting in line
2 is waiting in line
4 is waiting in line
5 is waiting in line
6 is waiting in line
7 is waiting in line
8 is waiting in line
9 is waiting in line
3 is waiting in line
2 is being served by the teller
0 is being served by the teller
4 is being served by the teller
1 is being served by the teller
4 is now leaving the teller
0 is now leaving the teller
2 is now leaving the teller
1 is now leaving the teller
7 is being served by the teller
5 is being served by the teller
8 is being served by the teller
6 is being served by the teller
8 is now leaving the teller
6 is now leaving the teller
7 is now leaving the teller
5 is now leaving the teller
9 is being served by the teller
3 is being served by the teller
9 is now leaving the teller
3 is now leaving the teller
Notify
use std::sync::Arc;
use tokio::sync::Notify;
use tokio::time::{sleep,Duration};
asyncfnorder_package(package_delivered: Arc<Notify>){
sleep(Duration::from_secs(2)).await;
println!("Find package in warehouse");
sleep(Duration::from_secs(2)).await;
println!("Ship package");
sleep(Duration::from_secs(2)).await;
println!("Package has been delivered");
package_delivered.notify_one();
}
asyncfngrab_package(package_delivered: Arc<Notify>){
package_delivered.notified().await;
println!("Look outside house for package");
sleep(Duration::from_secs(2)).await;
println!("Grab package");
}
#[tokio::main]asyncfnmain(){
letpackage_delivered = Notify::new();
letpackage_delivered_arc = Arc::new(package_delivered);
letorder_package_handle = tokio::spawn(
order_package(package_delivered_arc.clone())
);
letgrab_package_handle = tokio::spawn(
grab_package(package_delivered_arc.clone())
);
order_package_handle.await.unwrap();
grab_package_handle.await.unwrap();
}
Find package in warehouse
Ship package
Package has been delivered
Look outside house for package
Grab package
Barrier
use std::sync::Arc;
use tokio::sync::{Barrier, BarrierWaitResult, Notify};
use tokio::time::{sleep, Duration};
asyncfnbarrier_example(barrier: Arc<Barrier>, notify: Arc<Notify>) -> BarrierWaitResult{
println!("Waiting at barrier");
letwait_result = barrier.wait().await;
println!("Passed through the barrier");
if wait_result.is_leader(){
notify.notify_one();
}
wait_result
}
#[tokio::main]asyncfnmain(){
lettotal_cans_needed = 12;
letbarrier = Arc::new(Barrier::new(total_cans_needed));
letnotify = Arc::new(Notify::new());
//To send the first batch of cans to the barrier
notify.notify_one();
letmut task_handles = Vec::new();
forcan_countin0..60{
if can_count % 12 == 0{
notify.notified().await;
// Give the barrier some time closesleep(Duration::from_millis(1)).await;
}
task_handles.push(tokio::spawn(
barrier_example(barrier.clone(), notify.clone())
))
}
letmut num_of_leaders = 0;
forhandlein task_handles{
letwait_result = handle.await.unwrap();
if wait_result.is_leader(){
num_of_leaders += 1;
}
}
println!("total num of leaders: {}", num_of_leaders);
}
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Waiting at barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
Passed through the barrier
total num of leaders: 5
RwLock
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::{sleep,Duration};
asyncfnread_from_document(id: i32, document: Arc<RwLock<String>>){
letreader = document.read().await;
println!("reader_{}: {}", id, reader);
}
asyncfnwrite_to_document(document: Arc<RwLock<String>>, new_string: &str){
letmut writer = document.write().await;
println!("write {}",new_string);
writer.push_str(new_string);
writer.push_str(" ");
}
#[tokio::main]asyncfnmain(){
letdocument = Arc::new(RwLock::new("".to_string()));
letmut handles = Vec::new();
fornew_stringin"I can read and write a b c d e f h i j k l m n o p q r s t u v w x y z"
.split_whitespace(){
handles.push(tokio::spawn(read_from_document(1, document.clone())));
handles.push(tokio::spawn(write_to_document(document.clone(), new_string)));
handles.push(tokio::spawn(read_from_document(2, document.clone())));
handles.push(tokio::spawn(read_from_document(3, document.clone())));
}
forhandlein handles {
handle.await.unwrap();
}
}
reader_1:
write I
reader_1: I
reader_2: I
reader_3: I
write can
reader_1: I can
reader_2: I can
reader_3: I can
write read
reader_1: I can read
reader_2: I can read
reader_3: I can read
write and
reader_1: I can read and
reader_2: I can read and
reader_3: I can read and
write write
reader_3: I can read and write
reader_2: I can read and write
write a
reader_1: I can read and write a
reader_2: I can read and write a
reader_1: I can read and write a
reader_3: I can read and write a
write c
reader_1: I can read and write a c
reader_2: I can read and write a c
reader_3: I can read and write a c
write d
reader_1: I can read and write a c d
reader_2: I can read and write a c d
reader_3: I can read and write a c d
write e
reader_1: I can read and write a c d e
reader_2: I can read and write a c d e
reader_3: I can read and write a c d e
write f
reader_2: I can read and write a c d e f
reader_3: I can read and write a c d e f
reader_1: I can read and write a c d e f
write h
reader_2: I can read and write a c d e f h
reader_1: I can read and write a c d e f h
write b
reader_2: I can read and write a c d e f h b
reader_3: I can read and write a c d e f h b
reader_3: I can read and write a c d e f h b
reader_1: I can read and write a c d e f h b
write i
reader_1: I can read and write a c d e f h b i
reader_3: I can read and write a c d e f h b i
reader_2: I can read and write a c d e f h b i
write j
reader_1: I can read and write a c d e f h b i j
reader_2: I can read and write a c d e f h b i j
reader_3: I can read and write a c d e f h b i j
write k
reader_1: I can read and write a c d e f h b i j k
reader_2: I can read and write a c d e f h b i j k
reader_3: I can read and write a c d e f h b i j k
write l
reader_1: I can read and write a c d e f h b i j k l
reader_2: I can read and write a c d e f h b i j k l
reader_3: I can read and write a c d e f h b i j k l
write m
reader_3: I can read and write a c d e f h b i j k l m
reader_1: I can read and write a c d e f h b i j k l m
reader_2: I can read and write a c d e f h b i j k l m
write n
reader_1: I can read and write a c d e f h b i j k l m n
reader_2: I can read and write a c d e f h b i j k l m n
reader_3: I can read and write a c d e f h b i j k l m n
write o
reader_2: I can read and write a c d e f h b i j k l m n o
reader_3: I can read and write a c d e f h b i j k l m n o
reader_1: I can read and write a c d e f h b i j k l m n o
write p
reader_1: I can read and write a c d e f h b i j k l m n o p
reader_2: I can read and write a c d e f h b i j k l m n o p
reader_3: I can read and write a c d e f h b i j k l m n o p
write q
reader_1: I can read and write a c d e f h b i j k l m n o p q
reader_2: I can read and write a c d e f h b i j k l m n o p q
reader_3: I can read and write a c d e f h b i j k l m n o p q
write r
reader_1: I can read and write a c d e f h b i j k l m n o p q r
reader_2: I can read and write a c d e f h b i j k l m n o p q r
reader_3: I can read and write a c d e f h b i j k l m n o p q r
write s
reader_1: I can read and write a c d e f h b i j k l m n o p q r s
reader_3: I can read and write a c d e f h b i j k l m n o p q r s
reader_1: I can read and write a c d e f h b i j k l m n o p q r s
reader_2: I can read and write a c d e f h b i j k l m n o p q r s
reader_3: I can read and write a c d e f h b i j k l m n o p q r s
write u
reader_1: I can read and write a c d e f h b i j k l m n o p q r s u
reader_2: I can read and write a c d e f h b i j k l m n o p q r s u
reader_3: I can read and write a c d e f h b i j k l m n o p q r s u
write v
reader_1: I can read and write a c d e f h b i j k l m n o p q r s u v
write w
reader_2: I can read and write a c d e f h b i j k l m n o p q r s u v w
reader_3: I can read and write a c d e f h b i j k l m n o p q r s u v w
reader_3: I can read and write a c d e f h b i j k l m n o p q r s u v w
reader_1: I can read and write a c d e f h b i j k l m n o p q r s u v w
reader_2: I can read and write a c d e f h b i j k l m n o p q r s u v w
write x
reader_1: I can read and write a c d e f h b i j k l m n o p q r s u v w x
write y
reader_2: I can read and write a c d e f h b i j k l m n o p q r s u v w x y
reader_3: I can read and write a c d e f h b i j k l m n o p q r s u v w x y
reader_1: I can read and write a c d e f h b i j k l m n o p q r s u v w x y
write z
reader_2: I can read and write a c d e f h b i j k l m n o p q r s u v w x y z
reader_3: I can read and write a c d e f h b i j k l m n o p q r s u v w x y z
reader_3: I can read and write a c d e f h b i j k l m n o p q r s u v w x y z
reader_2: I can read and write a c d e f h b i j k l m n o p q r s u v w x y z
write t
reader_2: I can read and write a c d e f h b i j k l m n o p q r s u v w x y z t
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?