Skip to content

Commit 5f4de71

Browse files
committed
guide-tasks: Simplify Arc usage to match Arc docs.
1 parent fbd8f4a commit 5f4de71

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

src/doc/guide-tasks.md

+11-27
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,9 @@ fn main() {
343343
let numbers_arc = Arc::new(numbers);
344344
345345
for num in range(1u, 10) {
346-
let (tx, rx) = channel();
347-
tx.send(numbers_arc.clone());
346+
let task_numbers = numbers_arc.clone();
348347
349348
spawn(proc() {
350-
let local_arc : Arc<Vec<f64>> = rx.recv();
351-
let task_numbers = &*local_arc;
352349
println!("{}-norm = {}", num, pnorm(task_numbers.as_slice(), num));
353350
});
354351
}
@@ -369,40 +366,27 @@ let numbers_arc=Arc::new(numbers);
369366
# }
370367
~~~
371368

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.
373372

374373
~~~
375374
# extern crate sync;
376375
# extern crate rand;
377376
# use sync::Arc;
377+
# fn pnorm(nums: &[f64], p: uint) -> f64 { 4.0 }
378378
# fn main() {
379379
# let numbers=Vec::from_fn(1000000, |_| rand::random::<f64>());
380380
# 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+
});
401387
# }
402388
~~~
403389

404-
and can use it as if it were local.
405-
406390
The `arc` module also implements Arcs around mutable data that are not covered here.
407391

408392
# Handling task failure

0 commit comments

Comments
 (0)