From 62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2 Mon Sep 17 00:00:00 2001 From: hybrid Date: Sat, 31 Oct 2026 03:54:43 +0300 Subject: add: 7 craits smartpointers interior_mutability threads mutex hello-async async_concurrency multi_futures --- src/async_concurrency/Cargo.toml | 7 +++ src/async_concurrency/src/main.rs | 104 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/async_concurrency/Cargo.toml create mode 100644 src/async_concurrency/src/main.rs (limited to 'src/async_concurrency') diff --git a/src/async_concurrency/Cargo.toml b/src/async_concurrency/Cargo.toml new file mode 100644 index 0000000..22ffac4 --- /dev/null +++ b/src/async_concurrency/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "async_concurrency" +version = "0.1.0" +edition = "2024" + +[dependencies] +trpl = "0.3.0" diff --git a/src/async_concurrency/src/main.rs b/src/async_concurrency/src/main.rs new file mode 100644 index 0000000..0feadc5 --- /dev/null +++ b/src/async_concurrency/src/main.rs @@ -0,0 +1,104 @@ +use std::time::Duration; +use trpl; + +fn main() { + trpl::block_on(async { + let handle = trpl::spawn_task(async { + for i in 1..10 { + println!("hi number {i} from the first task!"); + trpl::sleep(Duration::from_millis(500)).await; + } + }); + + for i in 1..5 { + println!("hi number {i} from the second task!"); + trpl::sleep(Duration::from_millis(500)).await; + } + + handle.await.unwrap(); + + let fut1 = async { + for i in 1..10 { + println!("hi number {i} from the third task!"); + trpl::sleep(Duration::from_millis(500)).await; + } + }.await; + + let fut2 = async { + for i in 1..5 { + println!("hi number {i} from the fourth task!"); + trpl::sleep(Duration::from_millis(500)).await; + } + }.await; + + let fut3 = async { + for i in 1..5 { + println!("hi number {i} from the fifth task!"); + trpl::sleep(Duration::from_millis(500)).await; + } + }.await; + + let (tx, mut rx) = trpl::channel(); + + let tx_fut = async move { + let vals = vec![ + String::from("hi"), + String::from("from"), + String::from("the"), + String::from("future"), + ]; + + for val in vals { + tx.send(val).unwrap(); + trpl::sleep(Duration::from_millis(500)).await; + } + }; + + let rx_fut = async { + while let Some(value) = rx.recv().await { + println!("received '{value}'"); + } + }; + + trpl::join(tx_fut, rx_fut).await; + + let (tx, mut rx) = trpl::channel(); + + let tx1 = tx.clone(); + let tx1_fut = async move { + let vals = vec![ + String::from("hi"), + String::from("from"), + String::from("the"), + String::from("future"), + ]; + + for val in vals { + tx1.send(val).unwrap(); + trpl::sleep(Duration::from_millis(500)).await; + } + }; + + let rx_fut = async { + while let Some(value) = rx.recv().await { + println!("received '{value}'"); + } + }; + + let tx_fut = async move { + let vals = vec![ + String::from("more"), + String::from("messages"), + String::from("for"), + String::from("you"), + ]; + + for val in vals { + tx.send(val).unwrap(); + trpl::sleep(Duration::from_millis(1500)).await; + } + }; + + trpl::join!(tx1_fut, tx_fut, rx_fut); + }); +} -- cgit v1.3.1