This commit is contained in:
Tirth Patel 2024-06-19 21:53:31 -02:30
parent b2fdc401d5
commit 9c06d3e305
7 changed files with 16 additions and 19 deletions

View File

@ -8,7 +8,6 @@
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a // Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::thread; use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -27,6 +26,7 @@ fn main() {
let mut results: Vec<u128> = vec![]; let mut results: Vec<u128> = vec![];
for handle in handles { for handle in handles {
// TODO: a struct is returned from thread::spawn, can you use it? // TODO: a struct is returned from thread::spawn, can you use it?
results.push(handle.join().unwrap());
} }
if results.len() != 10 { if results.len() != 10 {

View File

@ -7,9 +7,8 @@
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a // Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::sync::Arc; use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
@ -18,14 +17,14 @@ struct JobStatus {
} }
fn main() { fn main() {
let status = Arc::new(JobStatus { jobs_completed: 0 }); let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
let mut handles = vec![]; let mut handles = vec![];
for _ in 0..10 { for _ in 0..10 {
let status_shared = Arc::clone(&status); let status_shared = Arc::clone(&status);
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(250));
// TODO: You must take an action before you update a shared value // TODO: You must take an action before you update a shared value
status_shared.jobs_completed += 1; status_shared.lock().unwrap().jobs_completed += 1;
}); });
handles.push(handle); handles.push(handle);
} }
@ -34,6 +33,6 @@ fn main() {
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice // 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 // anything interesting in the output? Do you have to 'join' on all the
// handles? // handles?
println!("jobs completed {}", ???); println!("jobs completed {}", status.lock().unwrap().jobs_completed);
} }
} }

View File

@ -3,7 +3,6 @@
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a // Execute `rustlings hint threads3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::Arc; use std::sync::Arc;
@ -30,11 +29,12 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
let qc = Arc::new(q); let qc = Arc::new(q);
let qc1 = Arc::clone(&qc); let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc); let qc2 = Arc::clone(&qc);
let tx1 = tx.clone();
thread::spawn(move || { thread::spawn(move || {
for val in &qc1.first_half { for val in &qc1.first_half {
println!("sending {:?}", val); println!("sending {:?}", val);
tx.send(*val).unwrap(); tx1.send(*val).unwrap();
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
} }
}); });

View File

@ -3,7 +3,6 @@
// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a // Execute `rustlings hint macros1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
@ -12,5 +11,5 @@ macro_rules! my_macro {
} }
fn main() { fn main() {
my_macro(); my_macro!();
} }

View File

@ -3,14 +3,14 @@
// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a // Execute `rustlings hint macros2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
fn main() {
my_macro!();
}
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
}; };
} }
fn main() {
my_macro!();
}

View File

@ -5,9 +5,9 @@
// Execute `rustlings hint macros3` or use the `hint` watch subcommand for a // Execute `rustlings hint macros3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
mod macros { mod macros {
#[macro_export]
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");

View File

@ -3,16 +3,15 @@
// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a // Execute `rustlings hint macros4` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
#[rustfmt::skip] #[rustfmt::skip]
macro_rules! my_macro { macro_rules! my_macro {
() => { () => {
println!("Check out my macro!"); println!("Check out my macro!");
} };
($val:expr) => { ($val:expr) => {
println!("Look at this other macro: {}", $val); println!("Look at this other macro: {}", $val);
} };
} }
fn main() { fn main() {