@@ -343,12 +343,9 @@ fn main() {
343
343
let numbers_arc = Arc::new(numbers);
344
344
345
345
for num in range(1u, 10) {
346
- let (tx, rx) = channel();
347
- tx.send(numbers_arc.clone());
346
+ let task_numbers = numbers_arc.clone();
348
347
349
348
spawn(proc() {
350
- let local_arc : Arc<Vec<f64>> = rx.recv();
351
- let task_numbers = &*local_arc;
352
349
println!("{}-norm = {}", num, pnorm(task_numbers.as_slice(), num));
353
350
});
354
351
}
@@ -369,40 +366,27 @@ let numbers_arc=Arc::new(numbers);
369
366
# }
370
367
~~~
371
368
372
- and a clone of it is sent to each task
369
+ and a unique clone is captured for each task via a procedure. This only copies the wrapper and not
370
+ it's contents. Within the task's procedure, the captured Arc reference can be used as an immutable
371
+ reference to the underlying vector as if it were local.
373
372
374
373
~~~
375
374
# extern crate sync;
376
375
# extern crate rand;
377
376
# use sync::Arc;
377
+ # fn pnorm(nums: &[f64], p: uint) -> f64 { 4.0 }
378
378
# fn main() {
379
379
# let numbers=Vec::from_fn(1000000, |_| rand::random::<f64>());
380
380
# let numbers_arc = Arc::new(numbers);
381
- # let (tx, rx) = channel();
382
- tx.send(numbers_arc.clone());
383
- # }
384
- ~~~
385
-
386
- copying only the wrapper and not its contents.
387
-
388
- Each task recovers the underlying data by
389
-
390
- ~~~
391
- # extern crate sync;
392
- # extern crate rand;
393
- # use sync::Arc;
394
- # fn main() {
395
- # let numbers=Vec::from_fn(1000000, |_| rand::random::<f64>());
396
- # let numbers_arc=Arc::new(numbers);
397
- # let (tx, rx) = channel();
398
- # tx.send(numbers_arc.clone());
399
- # let local_arc : Arc<Vec<f64>> = rx.recv();
400
- let task_numbers = &*local_arc;
381
+ # let num = 4;
382
+ let task_numbers = numbers_arc.clone();
383
+ spawn(proc() {
384
+ // Capture task_numbers and use it as if it was the underlying vector
385
+ println!("{}-norm = {}", num, pnorm(task_numbers.as_slice(), num));
386
+ });
401
387
# }
402
388
~~~
403
389
404
- and can use it as if it were local.
405
-
406
390
The ` arc ` module also implements Arcs around mutable data that are not covered here.
407
391
408
392
# Handling task failure
0 commit comments