diff options
| author | hybrid <hybrid@hybridlabs.cc> | 2026-10-31 03:54:43 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.cc> | 2026-10-31 03:54:43 +0300 |
| commit | 62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2 (patch) | |
| tree | b8461eda6cce3f9bbfb2261dd828077e17c7d226 /src/async_concurrency | |
| parent | 18ad654357d6cca34782aaa269f5e8e06cbe9241 (diff) | |
| download | rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.gz rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.tar.bz2 rust-book-62dcfb1d9f95503f8eb8be1b29fa1d75bfcc27e2.zip | |
add: 7 craits
smartpointers
interior_mutability
threads
mutex
hello-async
async_concurrency
multi_futures
Diffstat (limited to '')
| -rw-r--r-- | src/async_concurrency/Cargo.toml | 7 | ||||
| -rw-r--r-- | src/async_concurrency/src/main.rs | 104 |
2 files changed, 111 insertions, 0 deletions
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); + }); +} |
