Skip to content

Commit

Permalink
Solved threads exercises; Join handle, Arc and Mutex, and MPSC.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aziz Unsal authored and Aziz Unsal committed Nov 21, 2022
1 parent 7c25df7 commit 4f32be4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
8 changes: 2 additions & 6 deletions exercises/threads/threads1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@
// 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;
Expand All @@ -27,5 +24,4 @@ fn main() {
if completed_threads != 10 {
panic!("Oh no! All the spawned threads did not finish!");
}

}
15 changes: 9 additions & 6 deletions exercises/threads/threads2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,21 +12,26 @@ struct JobStatus {
}

fn main() {
let status = Arc::new(JobStatus { jobs_completed: 0 });
// 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 = Arc::clone(&status);
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;
// status_shared.jobs_completed += 1;
// [aziz]: BTW, this is an example of Rust's `Interior Mutability`.
status_shared.lock().unwrap().jobs_completed += 1;
});
handles.push(handle);
}
for handle in handles {
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 {}", ???);
// println!("jobs completed {}", ???);
}
let completed_jobs_cnt = status.lock().unwrap().jobs_completed;
println!("jobs completed= {}", completed_jobs_cnt);
}
6 changes: 3 additions & 3 deletions exercises/threads/threads3.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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::thread;
Expand All @@ -29,6 +27,8 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc);

// [aziz]: Let's clone the sending end(tx= transmitter) to have more than one transmitter.
let tx1 = tx.clone();
thread::spawn(move || {
for val in &qc1.first_half {
println!("sending {:?}", val);
Expand All @@ -40,7 +40,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
thread::spawn(move || {
for val in &qc2.second_half {
println!("sending {:?}", val);
tx.send(*val).unwrap();
tx1.send(*val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
Expand Down

0 comments on commit 4f32be4

Please sign in to comment.