This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use task::spawn; | |
| use pipes::{stream, SharedChan}; | |
| fn main() { | |
| // Create port/channel pair for receiving/sending ints, respectively | |
| let (port, chan) = stream(); | |
| // To allow multiple tasks to write, turn the channel into a SharedChan | |
| let chan = SharedChan(chan); | |
| fn doubler(x: int) -> int { x * 2 } | |
| // Spawn 3 tasks to process data in the background, | |
| // each writing to its own clone of the SharedChan | |
| let child = chan.clone(); | |
| do spawn { child.send(doubler(10)); } | |
| let child = chan.clone(); | |
| do spawn { child.send(doubler(20)); } | |
| let child = chan.clone(); | |
| do spawn { child.send((|a: int, b: int| a + b)(30, 40)); } | |
| // Make port/channel pair for the string result | |
| let (result_port, result_chan) = stream(); | |
| do spawn { | |
| let (x, y, z) = (port.recv(), port.recv(), port.recv()); | |
| let result = fmt!("%d + %d + %d = %d", x, y, z, x+y+z); | |
| result_chan.send(result); | |
| } | |
| // Receive and print result | |
| io::println(result_port.recv()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment