File tree Expand file tree Collapse file tree 1 file changed +11
-9
lines changed
Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Original file line number Diff line number Diff line change 1- // Mutex that protects the data vector, and then we spawn three threads
1+ // Mutex that protects the data vector, and then we spawn three threads
22//that each acquire a lock on the mutex and modify an element of the vector.
33
44use std:: sync:: Mutex ;
@@ -7,14 +7,16 @@ use std::thread;
77fn main ( ) {
88 let data = Mutex :: new ( vec ! [ 1 , 2 , 3 ] ) ;
99
10- let handles: Vec < _ > = ( 0 ..3 ) . map ( |i| {
11- let data = data. lock ( ) . unwrap ( ) . clone ( ) ;
12- thread:: spawn ( move || {
13- let mut data = data;
14- data[ i] += 1 ;
15- println ! ( "{:?}" , data) ;
10+ let handles: Vec < _ > = ( 0 ..3 )
11+ . map ( |i| {
12+ let data = data. lock ( ) . unwrap ( ) . clone ( ) ;
13+ thread:: spawn ( move || {
14+ let mut data = data;
15+ data[ i] += 1 ;
16+ println ! ( "{:?}" , data) ;
17+ } )
1618 } )
17- } ) . collect ( ) ;
19+ . collect ( ) ;
1820
1921 for handle in handles {
2022 handle. join ( ) . unwrap ( ) ;
@@ -41,4 +43,4 @@ fn main() {
4143
4244 // No data race can occur, this will not compile.
4345}
44- */
46+ */
You can’t perform that action at this time.
0 commit comments