Compare commits
4 Commits
ba887b2e97
...
b94ab30230
Author | SHA1 | Date | |
---|---|---|---|
b94ab30230 | |||
0fd24e1c3a | |||
5b95635674 | |||
06535d8348 |
2121
ch17/concurrency-with-async/Cargo.lock
generated
2121
ch17/concurrency-with-async/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
trpl = "0.2.0"
|
||||||
|
@ -1,6 +1,85 @@
|
|||||||
//! Stopped here:
|
//! Concurrency With Async
|
||||||
//! https://rust-book.cs.brown.edu/ch17-02-concurrency-with-async.html
|
//!
|
||||||
|
//! Similar to working with threads.
|
||||||
|
|
||||||
|
extern crate trpl;
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
// trpl::run(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;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Makes it so the program runs until both of these loops are
|
||||||
|
// // finished.
|
||||||
|
// handle.await.unwrap();
|
||||||
|
// });
|
||||||
|
|
||||||
|
trpl::run(async {
|
||||||
|
let fut1 = async {
|
||||||
|
for i in 1..10 {
|
||||||
|
println!("hi number {i} from the first task!");
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let fut2 = async {
|
||||||
|
for i in 1..5 {
|
||||||
|
println!("hi number {i} from the second task!");
|
||||||
|
trpl::sleep(Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// `trpl::join` function is fair meaning: checks each
|
||||||
|
// future equally often, alternating between them, never
|
||||||
|
// lets one race ahead if the other is ready.
|
||||||
|
trpl::join(fut1, fut2).await;
|
||||||
|
|
||||||
|
// message passing
|
||||||
|
//
|
||||||
|
// Can also share data between futures like this.
|
||||||
|
|
||||||
|
// async version of mutliple-producer, single consumer.
|
||||||
|
// trpl::run(async {
|
||||||
|
let (tx, mut rx) = trpl::channel();
|
||||||
|
|
||||||
|
let tx_fut = async {
|
||||||
|
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 val = String::from("hi");
|
||||||
|
// tx.send(val).unwrap();
|
||||||
|
|
||||||
|
// let received = rx.recv().await.unwrap();
|
||||||
|
// println!("Got: {received}");
|
||||||
|
// });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
{
|
{
|
||||||
devShells."${system}".default =
|
devShells."${system}".default =
|
||||||
pkgs.mkShellNoCC {
|
pkgs.mkShellNoCC {
|
||||||
|
nativeBuildInputs = [ pkgs.pkg-config ];
|
||||||
buildInputs = [ pkgs.bashInteractive ];
|
buildInputs = [ pkgs.bashInteractive ];
|
||||||
packages = with pkgs; [
|
packages = with pkgs; [
|
||||||
rustc
|
rustc
|
||||||
@ -20,6 +21,7 @@
|
|||||||
rust-analyzer
|
rust-analyzer
|
||||||
lldb
|
lldb
|
||||||
jetbrains.rust-rover
|
jetbrains.rust-rover
|
||||||
|
openssl
|
||||||
(vscode-with-extensions.override {
|
(vscode-with-extensions.override {
|
||||||
vscode = vscodium;
|
vscode = vscodium;
|
||||||
vscodeExtensions = with vscode-extensions; [
|
vscodeExtensions = with vscode-extensions; [
|
||||||
@ -27,6 +29,7 @@
|
|||||||
];
|
];
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
|
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
export SHELL=${pkgs.lib.getExe pkgs.bashInteractive}
|
export SHELL=${pkgs.lib.getExe pkgs.bashInteractive}
|
||||||
'';
|
'';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user