Skip to content

Commit 0895f72

Browse files
author
Olivier Saut
committed
Add a small section on futures to the tutorial, remove use task::spawn as it is now included by default in prelude.rs
1 parent 589d2c7 commit 0895f72

File tree

1 file changed

+59
-13
lines changed

1 file changed

+59
-13
lines changed

doc/tutorial-tasks.md

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,24 @@ in the core and standard libraries, which are still under development
4343
and do not always present a consistent or complete interface.
4444

4545
For your reference, these are the standard modules involved in Rust
46-
concurrency at this writing.
46+
concurrency at this writing:
4747

48-
* [`core::task`] - All code relating to tasks and task scheduling
49-
* [`core::comm`] - The message passing interface
50-
* [`core::pipes`] - The underlying messaging infrastructure
51-
* [`std::comm`] - Additional messaging types based on `core::pipes`
52-
* [`std::sync`] - More exotic synchronization tools, including locks
48+
* [`core::task`] - All code relating to tasks and task scheduling,
49+
* [`core::comm`] - The message passing interface,
50+
* [`core::pipes`] - The underlying messaging infrastructure,
51+
* [`std::comm`] - Additional messaging types based on `core::pipes`,
52+
* [`std::sync`] - More exotic synchronization tools, including locks,
5353
* [`std::arc`] - The ARC (atomically reference counted) type,
54-
for safely sharing immutable data
54+
for safely sharing immutable data,
55+
* [`std::future`] - A type representing values that may be computed concurrently and retrieved at a later time.
5556

5657
[`core::task`]: core/task.html
5758
[`core::comm`]: core/comm.html
5859
[`core::pipes`]: core/pipes.html
5960
[`std::comm`]: std/comm.html
6061
[`std::sync`]: std/sync.html
6162
[`std::arc`]: std/arc.html
63+
[`std::future`]: std/future.html
6264

6365
# Basics
6466

@@ -70,7 +72,7 @@ closure in the new task.
7072

7173
~~~~
7274
# use core::io::println;
73-
use core::task::spawn;
75+
# use core::task::spawn;
7476
7577
// Print something profound in a different task using a named function
7678
fn print_message() { println("I am running in a different task!"); }
@@ -145,8 +147,8 @@ endpoint. Consider the following example of calculating two results
145147
concurrently:
146148

147149
~~~~
148-
use core::task::spawn;
149-
use core::comm::{stream, Port, Chan};
150+
# use core::task::spawn;
151+
# use core::comm::{stream, Port, Chan};
150152
151153
let (port, chan): (Port<int>, Chan<int>) = stream();
152154
@@ -233,7 +235,8 @@ Instead we can use a `SharedChan`, a type that allows a single
233235

234236
~~~
235237
# use core::task::spawn;
236-
use core::comm::{stream, SharedChan};
238+
# use core::comm::{stream, SharedChan};
239+
use core::comm::SharedChan;
237240
238241
let (port, chan) = stream();
239242
let chan = SharedChan::new(chan);
@@ -282,6 +285,49 @@ let result = ports.foldl(0, |accum, port| *accum + port.recv() );
282285
# fn some_expensive_computation(_i: uint) -> int { 42 }
283286
~~~
284287

288+
## Futures
289+
With `std::future`, rust has a mechanism for requesting a computation and getting the result
290+
later.
291+
292+
The basic example below illustrates this.
293+
~~~
294+
fn fib(n: uint) -> uint {
295+
// lengthy computation returning an uint
296+
}
297+
298+
let mut delayed_fib = future::spawn (|| fib(5000) );
299+
make_a_sandwich();
300+
println(fmt!("fib(5000) = %?", delayed_fib.get()))
301+
~~~
302+
303+
The call to `future::spawn` returns immediately a `future` object regardless of how long it
304+
takes to run `fib(5000)`. You can then make yourself a sandwich while the computation of `fib` is
305+
running. The result of the execution of the method is obtained by calling `get` on the future.
306+
This call will block until the value is available (*i.e.* the computation is complete). Note that
307+
the future needs to be mutable so that it can save the result for next time `get` is called.
308+
309+
Here is another example showing how futures allow you to background computations. The workload will
310+
be distributed on the available cores.
311+
~~~
312+
fn partial_sum(start: uint) -> f64 {
313+
let mut local_sum = 0f64;
314+
for uint::range(start*100000, (start+1)*100000) |num| {
315+
local_sum += (num as f64 + 1).pow(-2.0);
316+
}
317+
local_sum
318+
}
319+
320+
fn main() {
321+
let mut futures = vec::from_fn(1000, |ind| do std::future::spawn { partial_sum(ind) });
322+
323+
let mut final_res = 0f64;
324+
for futures.each_mut |ft| {
325+
final_res += ft.get();
326+
}
327+
println(fmt!("π^2/6 is not far from : %?", final_res));
328+
}
329+
~~~
330+
285331
# Handling task failure
286332

287333
Rust has a built-in mechanism for raising exceptions. The `fail!()` macro
@@ -363,8 +409,8 @@ either task fails, it kills the other one.
363409
~~~
364410
# fn sleep_forever() { loop { task::yield() } }
365411
# do task::try {
366-
do task::spawn {
367-
do task::spawn {
412+
do spawn {
413+
do spawn {
368414
fail!(); // All three tasks will fail.
369415
}
370416
sleep_forever(); // Will get woken up by force, then fail

0 commit comments

Comments
 (0)