mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 21:59:18 +00:00
feat: 🎸 added solutions for threads exercises
This commit is contained in:
parent
aceba8071d
commit
8fd15ed6f6
@ -2,20 +2,16 @@
|
||||
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a hint.
|
||||
// This program should wait until all the spawned threads have finished before exiting.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
fn main() {
|
||||
|
||||
let mut handles = vec![];
|
||||
for i in 0..10 {
|
||||
thread::spawn(move || {
|
||||
handles.push(thread::spawn(move || {
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
println!("thread {} is complete", i);
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
let mut completed_threads = 0;
|
||||
@ -27,5 +23,4 @@ fn main() {
|
||||
if completed_threads != 10 {
|
||||
panic!("Oh no! All the spawned threads did not finish!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -3,9 +3,7 @@
|
||||
// Building on the last exercise, we want all of the threads to complete their work but this time
|
||||
// the spawned threads need to be in charge of updating a shared value: JobStatus.jobs_completed
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -14,14 +12,15 @@ struct JobStatus {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let status = Arc::new(JobStatus { jobs_completed: 0 });
|
||||
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
|
||||
let mut handles = vec![];
|
||||
for _ in 0..10 {
|
||||
let status_shared = status.clone();
|
||||
let handle = thread::spawn(move || {
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
// TODO: You must take an action before you update a shared value
|
||||
status_shared.jobs_completed += 1;
|
||||
let mut status = status_shared.lock().unwrap();
|
||||
status.jobs_completed += 1;
|
||||
});
|
||||
handles.push(handle);
|
||||
}
|
||||
@ -29,6 +28,7 @@ fn main() {
|
||||
handle.join().unwrap();
|
||||
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice anything
|
||||
// interesting in the output? Do you have to 'join' on all the handles?
|
||||
println!("jobs completed {}", ???);
|
||||
let status = status.lock().unwrap();
|
||||
println!("jobs completed {}", status.jobs_completed);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,8 @@
|
||||
// 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::sync::Arc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -24,23 +22,27 @@ impl Queue {
|
||||
}
|
||||
}
|
||||
|
||||
fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
|
||||
fn send_tx(q: Queue, tx: Arc<Mutex<mpsc::Sender<u32>>>) -> () {
|
||||
let qc = Arc::new(q);
|
||||
let qc1 = qc.clone();
|
||||
let qc2 = qc.clone();
|
||||
|
||||
let tx1 = tx.clone();
|
||||
thread::spawn(move || {
|
||||
for val in &qc1.first_half {
|
||||
println!("sending {:?}", val);
|
||||
tx.send(*val).unwrap();
|
||||
println!("sending {:?}", &val);
|
||||
let sender = tx1.lock().unwrap();
|
||||
sender.send(*val).unwrap();
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
});
|
||||
|
||||
let tx2 = tx.clone();
|
||||
thread::spawn(move || {
|
||||
for val in &qc2.second_half {
|
||||
println!("sending {:?}", val);
|
||||
tx.send(*val).unwrap();
|
||||
let sender = tx2.lock().unwrap();
|
||||
sender.send(*val).unwrap();
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
});
|
||||
@ -51,6 +53,7 @@ fn main() {
|
||||
let queue = Queue::new();
|
||||
let queue_length = queue.length;
|
||||
|
||||
let tx = Arc::new(Mutex::new(tx));
|
||||
send_tx(queue, tx);
|
||||
|
||||
let mut total_received: u32 = 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user