@@ -2375,10 +2375,10 @@ module `task`. Let's begin with the simplest one, `task::spawn()`:
2375
2375
2376
2376
~~~~
2377
2377
let some_value = 22;
2378
- let child_task = task::spawn {||
2378
+ task::spawn {||
2379
2379
std::io::println("This executes in the child task.");
2380
2380
std::io::println(#fmt("%d", some_value));
2381
- };
2381
+ }
2382
2382
~~~~
2383
2383
2384
2384
The argument to ` task::spawn() ` is a [ unique
@@ -2456,70 +2456,66 @@ let result = comm::recv(port);
2456
2456
## Creating a task with a bi-directional communication path
2457
2457
2458
2458
A very common thing to do is to spawn a child task where the parent
2459
- and child both need to exchange messages with each other. The function
2460
- ` task::spawn_connected () ` supports this pattern. We'll look briefly at
2461
- how it is used.
2459
+ and child both need to exchange messages with each
2460
+ other. The function ` task::spawn_listener () ` supports this pattern. We'll look
2461
+ briefly at how it is used.
2462
2462
2463
- To see how ` spawn_connected ()` works, we will create a child task
2463
+ To see how ` spawn_listener ()` works, we will create a child task
2464
2464
which receives ` uint ` messages, converts them to a string, and sends
2465
2465
the string in response. The child terminates when ` 0 ` is received.
2466
2466
Here is the function which implements the child task:
2467
2467
2468
2468
~~~~
2469
- fn stringifier(from_par : comm::port<uint>,
2470
- to_par : comm::chan<str>) {
2469
+ fn stringifier(from_parent : comm::port<uint>,
2470
+ to_parent : comm::chan<str>) {
2471
2471
let value: uint;
2472
2472
do {
2473
- value = comm::recv(from_par );
2474
- comm::send(to_par , uint::to_str(value, 10u));
2473
+ value = comm::recv(from_parent );
2474
+ comm::send(to_parent , uint::to_str(value, 10u));
2475
2475
} while value != 0u;
2476
2476
}
2477
2477
2478
2478
~~~~
2479
+
2479
2480
You can see that the function takes two parameters. The first is a
2480
2481
port used to receive messages from the parent, and the second is a
2481
2482
channel used to send messages to the parent. The body itself simply
2482
2483
loops, reading from the ` from_par ` port and then sending its response
2483
2484
to the ` to_par ` channel. The actual response itself is simply the
2484
2485
strified version of the received value, ` uint::to_str(value) ` .
2485
-
2486
+
2486
2487
Here is the code for the parent task:
2488
+
2487
2489
~~~~
2488
- # fn stringifier(from_par : comm::port<uint>,
2489
- # to_par : comm::chan<str>) {
2490
- # comm::send(to_par , "22");
2491
- # comm::send(to_par , "23");
2492
- # comm::send(to_par , "0");
2490
+ # fn stringifier(from_parent : comm::port<uint>,
2491
+ # to_parent : comm::chan<str>) {
2492
+ # comm::send(to_parent , "22");
2493
+ # comm::send(to_parent , "23");
2494
+ # comm::send(to_parent , "0");
2493
2495
# }
2494
2496
fn main() {
2495
- let t = task::spawn_connected(stringifier);
2496
- comm::send(t.to_child, 22u);
2497
- assert comm::recv(t.from_child) == "22";
2498
- comm::send(t.to_child, 23u);
2499
- assert comm::recv(t.from_child) == "23";
2500
- comm::send(t.to_child, 0u);
2501
- assert comm::recv(t.from_child) == "0";
2502
- }
2503
- ~~~~
2504
-
2505
- The call to ` spawn_connected() ` on the first line will instantiate the
2506
- various ports and channels and startup the child task. The returned
2507
- value, ` t ` , is a record of type ` task::connected_task<uint,str> ` . In
2508
- addition to the task id of the child, this record defines two fields,
2509
- ` from_child ` and ` to_child ` , which contain the port and channel
2510
- respectively for communicating with the child. Those fields are used
2511
- here to send and receive three messages from the child task.
2512
-
2513
- ## Joining a task
2514
-
2515
- The function ` spawn_joinable() ` is used to spawn a task that can later
2516
- be joined. This is implemented by having the child task send a message
2517
- when it has completed (either successfully or by failing). Therefore,
2518
- ` spawn_joinable() ` returns a structure containing both the task ID and
2519
- the port where this message will be sent---this structure type is
2520
- called ` task::joinable_task ` . The structure can be passed to
2521
- ` task::join() ` , which simply blocks on the port, waiting to receive
2522
- the message from the child task.
2497
+ let from_child = comm::port();
2498
+ let to_parent = comm::chan(from_child);
2499
+ let to_child = task::spawn_listener {|from_parent|
2500
+ stringifier(from_parent, to_parent);
2501
+ };
2502
+ comm::send(to_child, 22u);
2503
+ assert comm::recv(from_child) == "22";
2504
+ comm::send(to_child, 23u);
2505
+ assert comm::recv(from_child) == "23";
2506
+ comm::send(to_child, 0u);
2507
+ assert comm::recv(from_child) == "0";
2508
+ }
2509
+ ~~~~
2510
+
2511
+ The parent first sets up a port to receive data from and a channel
2512
+ that the child can use to send data to that port. The call to
2513
+ ` spawn_listener() ` will spawn the child task, providing it with a port
2514
+ on which to receive data from its parent, and returning to the parent
2515
+ the associated channel. Finally, the closure passed to
2516
+ ` spawn_listener() ` that forms the body of the child task captures the
2517
+ ` to_parent ` channel in its environment, so both parent and child
2518
+ can send and receive data to and from the other.
2523
2519
2524
2520
## The supervisor relationship
2525
2521
0 commit comments