File tree Expand file tree Collapse file tree 2 files changed +6
-8
lines changed
Expand file tree Collapse file tree 2 files changed +6
-8
lines changed Original file line number Diff line number Diff line change 88// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
99// hint.
1010
11- // I AM NOT DONE
12-
1311use std:: thread;
1412use std:: time:: { Duration , Instant } ;
1513
@@ -27,6 +25,7 @@ fn main() {
2725 let mut results: Vec < u128 > = vec ! [ ] ;
2826 for handle in handles {
2927 // TODO: a struct is returned from thread::spawn, can you use it?
28+ results. push ( handle. join ( ) . unwrap ( ) ) ;
3029 }
3130
3231 if results. len ( ) != 10 {
Original file line number Diff line number Diff line change 77// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
88// hint.
99
10- // I AM NOT DONE
11-
12- use std:: sync:: Arc ;
10+ use std:: sync:: { Arc , Mutex } ;
1311use std:: thread;
1412use std:: time:: Duration ;
1513
@@ -18,14 +16,15 @@ struct JobStatus {
1816}
1917
2018fn main ( ) {
21- let status = Arc :: new ( JobStatus { jobs_completed : 0 } ) ;
19+ let status = Arc :: new ( Mutex :: new ( JobStatus { jobs_completed : 0 } ) ) ;
2220 let mut handles = vec ! [ ] ;
2321 for _ in 0 ..10 {
2422 let status_shared = Arc :: clone ( & status) ;
2523 let handle = thread:: spawn ( move || {
2624 thread:: sleep ( Duration :: from_millis ( 250 ) ) ;
2725 // TODO: You must take an action before you update a shared value
28- status_shared. jobs_completed += 1 ;
26+ let mut s_status = status_shared. lock ( ) . unwrap ( ) ;
27+ s_status. jobs_completed += 1 ;
2928 } ) ;
3029 handles. push ( handle) ;
3130 }
@@ -34,6 +33,6 @@ fn main() {
3433 // TODO: Print the value of the JobStatus.jobs_completed. Did you notice
3534 // anything interesting in the output? Do you have to 'join' on all the
3635 // handles?
37- println ! ( "jobs completed {}" , ??? ) ;
36+ println ! ( "jobs completed {}" , status . lock ( ) . unwrap ( ) . jobs_completed ) ;
3837 }
3938}
You can’t perform that action at this time.
0 commit comments