Browse Source

Making a producer/consumer in Rust

master
Julio Biason 2 years ago
parent
commit
b20567db8e
  1. 7
      prodconrs/Cargo.lock
  2. 8
      prodconrs/Cargo.toml
  3. 31
      prodconrs/src/main.rs

7
prodconrs/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "prodconrs"
version = "0.1.0"

8
prodconrs/Cargo.toml

@ -0,0 +1,8 @@
[package]
name = "prodconrs"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

31
prodconrs/src/main.rs

@ -0,0 +1,31 @@
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
let self_tx = tx.clone();
let consumer = thread::spawn(move || {
while let Ok(msg) = rx.recv() {
if msg == 0 {
println!("Quit");
break;
}
println!("Message: {}", msg);
}
});
let producer = thread::spawn(move || {
for i in 1..12 {
tx.send(i);
}
tx.send(0);
});
println!("Waiting producer...");
producer.join();
println!("Waiting consumer...");
consumer.join();
}
Loading…
Cancel
Save