Skip to content

Commit 0a822c2

Browse files
committed
// threads3.rs
// // Execute `rustlings hint threads3` or use the `hint` watch subcommand for a // hint. // I AM NOT DONE // threads3.rs // // Execute `rustlings hint threads3` or use the `hint` watch subcommand for a // hint. // I AM NOT DONE use std::sync::mpsc; use std::thread; use std::time::Duration; struct Queue { length: u32, first_half: Vec<u32>, second_half: Vec<u32>, } impl Queue { fn new() -> Self { Queue { length: 10, first_half: vec![1, 2, 3, 4, 5], second_half: vec![6, 7, 8, 9, 10], } } } fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () { // 克隆 Sender 用于第二个线程 let tx2 = tx.clone(); // 第一个线程使用原始 tx 发送 first_half thread::spawn(move || { for val in q.first_half { println!("sending {:?}", val); tx.send(val).unwrap(); thread::sleep(Duration::from_secs(1)); } }); // 第二个线程使用克隆的 tx2 发送 second_half thread::spawn(move || { for val in q.second_half { println!("sending {:?}", val); tx2.send(val).unwrap(); thread::sleep(Duration::from_secs(1)); } }); } fn main() { let (tx, rx) = mpsc::channel(); let queue = Queue::new(); let queue_length = queue.length; send_tx(queue, tx); let mut total_received: u32 = 0; // 接收所有数据直到达到预期长度 for received in rx { println!("Got: {}", received); total_received += 1; if total_received == queue_length { break; } } println!("total numbers received: {}", total_received); assert_eq!(total_received, queue_length) }
1 parent f1a534f commit 0a822c2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

exercises/threads/threads3.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// I AM NOT DONE
1414

1515
use std::sync::mpsc;
16-
use std::sync::Arc;
1716
use std::thread;
1817
use std::time::Duration;
1918

@@ -34,22 +33,23 @@ impl Queue {
3433
}
3534

3635
fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
37-
let qc = Arc::new(q);
38-
let qc1 = Arc::clone(&qc);
39-
let qc2 = Arc::clone(&qc);
36+
// 克隆 Sender 用于第二个线程
37+
let tx2 = tx.clone();
4038

39+
// 第一个线程使用原始 tx 发送 first_half
4140
thread::spawn(move || {
42-
for val in &qc1.first_half {
41+
for val in q.first_half {
4342
println!("sending {:?}", val);
44-
tx.send(*val).unwrap();
43+
tx.send(val).unwrap();
4544
thread::sleep(Duration::from_secs(1));
4645
}
4746
});
4847

48+
// 第二个线程使用克隆的 tx2 发送 second_half
4949
thread::spawn(move || {
50-
for val in &qc2.second_half {
50+
for val in q.second_half {
5151
println!("sending {:?}", val);
52-
tx.send(*val).unwrap();
52+
tx2.send(val).unwrap();
5353
thread::sleep(Duration::from_secs(1));
5454
}
5555
});
@@ -63,9 +63,13 @@ fn main() {
6363
send_tx(queue, tx);
6464

6565
let mut total_received: u32 = 0;
66+
// 接收所有数据直到达到预期长度
6667
for received in rx {
6768
println!("Got: {}", received);
6869
total_received += 1;
70+
if total_received == queue_length {
71+
break;
72+
}
6973
}
7074

7175
println!("total numbers received: {}", total_received);

0 commit comments

Comments
 (0)