summaryrefslogtreecommitdiffstats
path: root/src/async_concurrency
diff options
context:
space:
mode:
Diffstat (limited to 'src/async_concurrency')
-rw-r--r--src/async_concurrency/Cargo.toml7
-rw-r--r--src/async_concurrency/src/main.rs104
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);
+ });
+}