Skip to content

Commit

Permalink
push 79-83 thread
Browse files Browse the repository at this point in the history
  • Loading branch information
imizao committed Apr 17, 2024
1 parent e7a9591 commit 7fd6e86
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
7 changes: 6 additions & 1 deletion exercises/smart_pointers/cow1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//
// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

use std::borrow::Cow;

Expand Down Expand Up @@ -49,6 +48,8 @@ mod tests {
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
// TODO
Cow::Borrowed(_) => Ok(()),
_ => Err("Expected borrowed value"),
}
}

Expand All @@ -61,6 +62,8 @@ mod tests {
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected borrowed value"),
}
}

Expand All @@ -73,6 +76,8 @@ mod tests {
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}
}
2 changes: 1 addition & 1 deletion exercises/threads/threads1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::thread;
use std::time::{Duration, Instant};
Expand All @@ -27,6 +26,7 @@ fn main() {
let mut results: Vec<u128> = vec![];
for handle in handles {
// TODO: a struct is returned from thread::spawn, can you use it?
results.push(handle.join().unwrap());
}

if results.len() != 10 {
Expand Down
11 changes: 6 additions & 5 deletions exercises/threads/threads2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

Expand All @@ -18,14 +17,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 = 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;
let mut status = status_shared.lock().unwrap();
status.jobs_completed += 1;
});
handles.push(handle);
}
Expand All @@ -34,6 +34,7 @@ fn main() {
// 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 {}", status.lock().unwrap().jobs_completed);
}
11 changes: 7 additions & 4 deletions exercises/threads/threads3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// 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;
Expand All @@ -28,21 +27,25 @@ impl Queue {

fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
let qc = Arc::new(q);

let tx1 = tx.clone();
let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc);
// let qc2 = Arc::clone(&qc);

thread::spawn(move || {
for val in &qc1.first_half {
println!("sending {:?}", val);
tx.send(*val).unwrap();
tx1.send(*val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});

let tx2 = tx.clone();
let qc2 = Arc::clone(&qc);
thread::spawn(move || {
for val in &qc2.second_half {
println!("sending {:?}", val);
tx.send(*val).unwrap();
tx2.send(*val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
Expand Down

0 comments on commit 7fd6e86

Please sign in to comment.