@@ -196,7 +196,7 @@ When complete, `make install` will place the following programs into
196
196
` /usr/local/bin ` :
197
197
198
198
* ` rustc ` , the Rust compiler
199
- * ` rustdoc ` , the API-documentation tool
199
+ * ` rustdoc ` , the API-documentation tool
200
200
* ` cargo ` , the Rust package manager
201
201
202
202
[ wiki-get-started ] : https://github.com/mozilla/rust/wiki/Note-getting-started-developing-Rust
@@ -2960,7 +2960,11 @@ do spawn {
2960
2960
~~~~
2961
2961
2962
2962
This child will perform the expensive computation send the result
2963
- over the channel. Finally, the parent continues by performing
2963
+ over the channel. (Under the hood, ` chan ` was captured by the
2964
+ closure that forms the body of the child task. This capture is
2965
+ allowed because channels are sendable.)
2966
+
2967
+ Finally, the parent continues by performing
2964
2968
some other expensive computation and then waiting for the child's result
2965
2969
to arrive on the port:
2966
2970
@@ -2978,10 +2982,10 @@ let result = port.recv();
2978
2982
2979
2983
A very common thing to do is to spawn a child task where the parent
2980
2984
and child both need to exchange messages with each
2981
- other. The function ` task::spawn_listener () ` supports this pattern. We'll look
2982
- briefly at how it is used.
2985
+ other. The function ` task::spawn_conversation () ` supports this pattern.
2986
+ We'll look briefly at how it is used.
2983
2987
2984
- To see how ` spawn_listener ()` works, we will create a child task
2988
+ To see how ` spawn_conversation ()` works, we will create a child task
2985
2989
that receives ` uint ` messages, converts them to a string, and sends
2986
2990
the string in response. The child terminates when ` 0 ` is received.
2987
2991
Here is the function that implements the child task:
@@ -3006,11 +3010,11 @@ loops, reading from the `from_parent` port and then sending its
3006
3010
response to the ` to_parent ` channel. The actual response itself is
3007
3011
simply the strified version of the received value,
3008
3012
` uint::to_str(value) ` .
3009
-
3013
+
3010
3014
Here is the code for the parent task:
3011
3015
3012
3016
~~~~
3013
- # import task::{spawn_listener };
3017
+ # import task::{spawn_conversation };
3014
3018
# import comm::{chan, port, methods};
3015
3019
# fn stringifier(from_parent: comm::port<uint>,
3016
3020
# to_parent: comm::chan<~str>) {
@@ -3020,32 +3024,30 @@ Here is the code for the parent task:
3020
3024
# }
3021
3025
# fn main() {
3022
3026
3023
- let from_child = port();
3024
- let to_parent = from_child.chan();
3025
- let to_child = do spawn_listener |from_parent| {
3027
+ let (from_child, to_child) = do spawn_conversation |from_parent, to_parent| {
3026
3028
stringifier(from_parent, to_parent);
3027
3029
};
3028
3030
3029
3031
to_child.send(22u);
3030
3032
assert from_child.recv() == ~"22";
3031
3033
3032
3034
to_child.send(23u);
3033
- assert from_child.recv() == ~"23";
3034
-
3035
3035
to_child.send(0u);
3036
+
3037
+ assert from_child.recv() == ~"23";
3036
3038
assert from_child.recv() == ~"0";
3037
3039
3038
3040
# }
3039
3041
~~~~
3040
3042
3041
- The parent first sets up a port to receive data from and a channel
3042
- that the child can use to send data to that port. The call to
3043
- ` spawn_listener() ` will spawn the child task, providing it with a port
3044
- on which to receive data from its parent, and returning to the parent
3045
- the associated channel. Finally, the closure passed to
3046
- ` spawn_listener() ` that forms the body of the child task captures the
3047
- ` to_parent ` channel in its environment, so both parent and child
3048
- can send and receive data to and from the other .
3043
+ The parent task calls ` spawn_conversation ` with a function that takes
3044
+ a ` from_parent ` port and a ` to_parent ` channel. In return, it gets a
3045
+ ` from_child ` channel and a ` to_child ` port. As a result, both parent
3046
+ and child can send and receive data to and from the other.
3047
+
3048
+ ` spawn_conversation `
3049
+ will create two port/channel pairs, passing one set to the child task
3050
+ and returning the other set to the caller .
3049
3051
3050
3052
# Testing
3051
3053
0 commit comments