-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use anyhow::{anyhow, Result}; | ||
use std::{sync::mpsc, thread, time::Duration}; | ||
|
||
const NUM_PRODUCERS: usize = 4; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug)] | ||
struct Msg { | ||
idx: usize, | ||
value: usize, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let (tx, rx) = mpsc::channel(); | ||
|
||
// 创建 producers | ||
for i in 0..NUM_PRODUCERS { | ||
let tx = tx.clone(); | ||
thread::spawn(move || producer(i, tx)); | ||
} | ||
drop(tx); // 释放 tx,否则 rx 无法结束 | ||
|
||
// 创建 consumer | ||
let consumer = thread::spawn(move || { | ||
for msg in rx { | ||
println!("consumer: {:?}", msg); | ||
} | ||
println!("consumer exit"); | ||
42 | ||
}); | ||
|
||
let secret = consumer | ||
.join() | ||
.map_err(|e| anyhow!("Thread join error: {:?}", e))?; | ||
|
||
println!("secret: {}", secret); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn producer(idx: usize, tx: mpsc::Sender<Msg>) -> Result<()> { | ||
loop { | ||
let value = rand::random::<usize>(); | ||
tx.send(Msg::new(idx, value))?; | ||
let sleep_time = rand::random::<u8>() as u64 * 10; | ||
thread::sleep(Duration::from_millis(sleep_time)); | ||
// random exit the producer | ||
if rand::random::<u8>() % 5 == 0 { | ||
println!("producer {} exit", idx); | ||
break; | ||
} | ||
} | ||
// more things to do | ||
Ok(()) | ||
} | ||
|
||
impl Msg { | ||
fn new(idx: usize, value: usize) -> Self { | ||
Self { idx, value } | ||
} | ||
} |