mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 13:49:19 +00:00
threads
This commit is contained in:
parent
262dd69640
commit
ae18f92657
@ -2,30 +2,26 @@
|
|||||||
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a hint.
|
// 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.
|
// This program should wait until all the spawned threads have finished before exiting.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
thread::spawn(move || {
|
handles.push(thread::spawn(move || {
|
||||||
thread::sleep(Duration::from_millis(250));
|
thread::sleep(Duration::from_millis(250));
|
||||||
println!("thread {} is complete", i);
|
println!("thread {} is complete", i);
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut completed_threads = 0;
|
let mut completed_threads = 0;
|
||||||
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?
|
||||||
|
handle.join();
|
||||||
completed_threads += 1;
|
completed_threads += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if completed_threads != 10 {
|
if completed_threads != 10 {
|
||||||
panic!("Oh no! All the spawned threads did not finish!");
|
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
|
// 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
|
// the spawned threads need to be in charge of updating a shared value: JobStatus.jobs_completed
|
||||||
|
|
||||||
// I AM NOT DONE
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@ -14,14 +12,15 @@ 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 = status.clone();
|
||||||
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;
|
let mut s_shared = status_shared.lock().unwrap();
|
||||||
|
s_shared.jobs_completed += 1;
|
||||||
});
|
});
|
||||||
handles.push(handle);
|
handles.push(handle);
|
||||||
}
|
}
|
||||||
@ -29,6 +28,6 @@ fn main() {
|
|||||||
handle.join().unwrap();
|
handle.join().unwrap();
|
||||||
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice anything
|
// 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?
|
// interesting in the output? Do you have to 'join' on all the handles?
|
||||||
println!("jobs completed {}", ???);
|
println!("jobs completed {}", status.lock().unwrap().jobs_completed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
// threads3.rs
|
// threads3.rs
|
||||||
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
@ -26,13 +24,14 @@ impl Queue {
|
|||||||
|
|
||||||
fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
|
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 = qc.clone();
|
||||||
let qc2 = Arc::clone(&qc);
|
let qc2 = qc.clone();
|
||||||
|
let trans1 = tx.clone();
|
||||||
|
let trans2 = 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();
|
trans1.clone().send(*val).unwrap();
|
||||||
thread::sleep(Duration::from_secs(1));
|
thread::sleep(Duration::from_secs(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -40,7 +39,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
|
|||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
for val in &qc2.second_half {
|
for val in &qc2.second_half {
|
||||||
println!("sending {:?}", val);
|
println!("sending {:?}", val);
|
||||||
tx.send(*val).unwrap();
|
trans2.send(*val).unwrap();
|
||||||
thread::sleep(Duration::from_secs(1));
|
thread::sleep(Duration::from_secs(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user