@@ -263,6 +263,7 @@ With `extra::future`, rust has a mechanism for requesting a computation and gett
263
263
later.
264
264
265
265
The basic example below illustrates this.
266
+
266
267
~~~
267
268
# fn make_a_sandwich() {};
268
269
fn fib(n: u64) -> u64 {
@@ -283,6 +284,7 @@ the future needs to be mutable so that it can save the result for next time `get
283
284
284
285
Here is another example showing how futures allow you to background computations. The workload will
285
286
be distributed on the available cores.
287
+
286
288
~~~
287
289
# use std::vec;
288
290
fn partial_sum(start: uint) -> f64 {
@@ -317,6 +319,7 @@ acts as a reference to the shared data and only this reference is shared and clo
317
319
318
320
Here is a small example showing how to use Arcs. We wish to run concurrently several computations on
319
321
a single large vector of floats. Each task needs the full vector to perform its duty.
322
+
320
323
~~~
321
324
# use std::vec;
322
325
# use std::rand;
@@ -348,14 +351,17 @@ fn main() {
348
351
The function ` pnorm ` performs a simple computation on the vector (it computes the sum of its items
349
352
at the power given as argument and takes the inverse power of this value). The Arc on the vector is
350
353
created by the line
354
+
351
355
~~~
352
356
# use extra::arc::Arc;
353
357
# use std::vec;
354
358
# use std::rand;
355
359
# let numbers = vec::from_fn(1000000, |_| rand::random::<f64>());
356
360
let numbers_arc=Arc::new(numbers);
357
361
~~~
362
+
358
363
and a clone of it is sent to each task
364
+
359
365
~~~
360
366
# use extra::arc::Arc;
361
367
# use std::vec;
@@ -365,9 +371,11 @@ and a clone of it is sent to each task
365
371
# let (port, chan) = Chan::new();
366
372
chan.send(numbers_arc.clone());
367
373
~~~
374
+
368
375
copying only the wrapper and not its contents.
369
376
370
377
Each task recovers the underlying data by
378
+
371
379
~~~
372
380
# use extra::arc::Arc;
373
381
# use std::vec;
@@ -379,6 +387,7 @@ Each task recovers the underlying data by
379
387
# let local_arc : Arc<~[f64]> = port.recv();
380
388
let task_numbers = local_arc.get();
381
389
~~~
390
+
382
391
and can use it as if it were local.
383
392
384
393
The ` arc ` module also implements Arcs around mutable data that are not covered here.
0 commit comments