Compare commits
No commits in common. "b94ab302303f99a0a0db9a385d0cba4615c2e354" and "ba887b2e972f9d5e92e3bb0f9847d27ad8f1c721" have entirely different histories.
b94ab30230
...
ba887b2e97
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,4 +4,3 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
trpl = "0.2.0"
|
|
||||||
|
@ -1,85 +1,6 @@
|
|||||||
//! Concurrency With Async
|
//! Stopped here:
|
||||||
//!
|
//! 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() {
|
||||||
// trpl::run(async {
|
println!("Hello, world!");
|
||||||
// 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,7 +10,6 @@
|
|||||||
{
|
{
|
||||||
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
|
||||||
@ -21,7 +20,6 @@
|
|||||||
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; [
|
||||||
@ -29,7 +27,6 @@
|
|||||||
];
|
];
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
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