File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed
Expand file tree Collapse file tree 1 file changed +6
-6
lines changed 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 ;
11+ use std:: sync:: Mutex ;
1312use std:: thread;
1413use std:: time:: Duration ;
1514
@@ -18,14 +17,15 @@ struct JobStatus {
1817}
1918
2019fn main ( ) {
21- let status = Arc :: new ( JobStatus { jobs_completed : 0 } ) ;
20+ let status = Arc :: new ( Mutex :: new ( JobStatus { jobs_completed : 0 } ) ) ;
2221 let mut handles = vec ! [ ] ;
2322 for _ in 0 ..10 {
2423 let status_shared = Arc :: clone ( & status) ;
2524 let handle = thread:: spawn ( move || {
2625 thread:: sleep ( Duration :: from_millis ( 250 ) ) ;
2726 // TODO: You must take an action before you update a shared value
28- status_shared. jobs_completed += 1 ;
27+ let mut status_guard = status_shared. lock ( ) . unwrap ( ) ;
28+ status_guard. jobs_completed += 1 ;
2929 } ) ;
3030 handles. push ( handle) ;
3131 }
@@ -34,6 +34,6 @@ fn main() {
3434 // TODO: Print the value of the JobStatus.jobs_completed. Did you notice
3535 // anything interesting in the output? Do you have to 'join' on all the
3636 // handles?
37- println ! ( "jobs completed {}" , ??? ) ;
37+ println ! ( "jobs completed {}" , status . lock ( ) . unwrap ( ) . jobs_completed ) ;
3838 }
3939}
You can’t perform that action at this time.
0 commit comments