@@ -51,13 +51,15 @@ closure is limited to capturing `Send`-able data from its environment
5151ensures that ` spawn ` can safely move the entire closure and all its
5252associated state into an entirely different thread for execution.
5353
54- ``` {rust,ignore}
55- # use std::thread::spawn;
56- # fn generate_thread_number() -> int { 0 }
54+ ``` rust
55+ use std :: thread :: Thread ;
56+
57+ fn generate_thread_number () -> i32 { 4 } // a very simple generation
58+
5759// Generate some state locally
5860let child_thread_number = generate_thread_number ();
5961
60- spawn(move || {
62+ Thread :: spawn (move || {
6163 // Capture it in the remote thread. The `move` keyword indicates
6264 // that this closure should move `child_thread_number` into its
6365 // environment, rather than capturing a reference into the
@@ -77,40 +79,44 @@ The simplest way to create a channel is to use the `channel` function to create
7779of a channel, and a * receiver* is the receiving endpoint. Consider the following
7880example of calculating two results concurrently:
7981
80- ``` {rust,ignore}
81- # use std::thread::spawn;
82+ ``` rust
83+ use std :: thread :: Thread ;
84+ use std :: sync :: mpsc;
8285
83- let (tx, rx): (Sender<int >, Receiver<int >) = channel();
86+ let (tx , rx ): (mpsc :: Sender <u32 >, mpsc :: Receiver <u32 >) = mpsc :: channel ();
8487
85- spawn(move || {
88+ Thread :: spawn (move || {
8689 let result = some_expensive_computation ();
8790 tx . send (result );
8891});
8992
9093some_other_expensive_computation ();
9194let result = rx . recv ();
92- # fn some_expensive_computation() -> int { 42 }
93- # fn some_other_expensive_computation() {}
95+
96+ fn some_expensive_computation () -> u32 { 42 } // very expensive ;)
97+ fn some_other_expensive_computation () {} // even more so
9498```
9599
96100Let's examine this example in detail. First, the ` let ` statement creates a
97101stream for sending and receiving integers (the left-hand side of the ` let ` ,
98102` (tx, rx) ` , is an example of a destructuring let: the pattern separates a tuple
99103into its component parts).
100104
101- ``` {rust,ignore}
102- let (tx, rx): (Sender<int>, Receiver<int>) = channel();
105+ ``` rust
106+ # use std :: sync :: mpsc;
107+ let (tx , rx ): (mpsc :: Sender <u32 >, mpsc :: Receiver <u32 >) = mpsc :: channel ();
103108```
104109
105110The child thread will use the sender to send data to the parent thread, which will
106111wait to receive the data on the receiver. The next statement spawns the child
107112thread.
108113
109- ``` {rust,ignore}
110- # use std::thread::spawn;
111- # fn some_expensive_computation() -> int { 42 }
112- # let (tx, rx) = channel();
113- spawn(move || {
114+ ``` rust
115+ # use std :: thread :: Thread ;
116+ # use std :: sync :: mpsc;
117+ # fn some_expensive_computation () -> u32 { 42 }
118+ # let (tx , rx ) = mpsc :: channel ();
119+ Thread :: spawn (move || {
114120 let result = some_expensive_computation ();
115121 tx . send (result );
116122});
@@ -125,9 +131,10 @@ computation, then sends the result over the captured channel.
125131Finally, the parent continues with some other expensive computation, then waits
126132for the child's result to arrive on the receiver:
127133
128- ``` {rust,ignore}
134+ ``` rust
135+ # use std :: sync :: mpsc;
129136# fn some_other_expensive_computation () {}
130- # let (tx, rx) = channel::<int >();
137+ # let (tx , rx ) = mpsc :: channel :: <u32 >();
131138# tx . send (0 );
132139some_other_expensive_computation ();
133140let result = rx . recv ();
@@ -140,8 +147,9 @@ single `Receiver` value. What if our example needed to compute multiple
140147results across a number of threads? The following program is ill-typed:
141148
142149``` {rust,ignore}
143- # fn some_expensive_computation() -> int { 42 }
144- let (tx, rx) = channel();
150+ # use std::sync::mpsc;
151+ # fn some_expensive_computation() -> u32 { 42 }
152+ let (tx, rx) = mpsc::channel();
145153
146154spawn(move || {
147155 tx.send(some_expensive_computation());
@@ -156,19 +164,22 @@ spawn(move || {
156164
157165Instead we can clone the ` tx ` , which allows for multiple senders.
158166
159- ``` {rust,ignore}
160- let (tx, rx) = channel();
167+ ``` rust
168+ use std :: thread :: Thread ;
169+ use std :: sync :: mpsc;
170+
171+ let (tx , rx ) = mpsc :: channel ();
161172
162- for init_val in range(0u, 3) {
173+ for init_val in 0 .. 3 {
163174 // Create a new channel handle to distribute to the child thread
164175 let child_tx = tx . clone ();
165- spawn(move || {
176+ Thread :: spawn (move || {
166177 child_tx . send (some_expensive_computation (init_val ));
167178 });
168179}
169180
170- let result = rx.recv() + rx.recv() + rx.recv();
171- # fn some_expensive_computation(_i: uint ) -> int { 42 }
181+ let result = rx . recv (). unwrap () + rx . recv (). unwrap () + rx . recv () . unwrap ();
182+ # fn some_expensive_computation (_i : u32 ) -> u32 { 42 }
172183```
173184
174185Cloning a ` Sender ` produces a new handle to the same channel, allowing multiple
@@ -181,21 +192,22 @@ Note that the above cloning example is somewhat contrived since you could also
181192simply use three ` Sender ` pairs, but it serves to illustrate the point. For
182193reference, written with multiple streams, it might look like the example below.
183194
184- ``` {rust,ignore}
185- # use std::thread::spawn;
195+ ``` rust
196+ use std :: thread :: Thread ;
197+ use std :: sync :: mpsc;
186198
187199// Create a vector of ports, one for each child thread
188- let rxs = Vec::from_fn(3, | init_val| {
189- let (tx, rx) = channel();
190- spawn(move || {
200+ let rxs = ( 0 .. 3 ) . map ( | & : init_val | {
201+ let (tx , rx ) = mpsc :: channel ();
202+ Thread :: spawn (move || {
191203 tx . send (some_expensive_computation (init_val ));
192204 });
193205 rx
194- });
206+ }). collect :: < Vec < _ >>() ;
195207
196208// Wait on each port, accumulating the results
197- let result = rxs.iter().fold(0, |accum, rx| accum + rx.recv() );
198- # fn some_expensive_computation(_i: uint ) -> int { 42 }
209+ let result = rxs . iter (). fold (0 , | & : accum , rx | accum + rx . recv () . unwrap () );
210+ # fn some_expensive_computation (_i : u32 ) -> u32 { 42 }
199211```
200212
201213## Backgrounding computations: Futures
0 commit comments