Created
October 5, 2012 10:34
-
-
Save reusee/3839158 to your computer and use it in GitHub Desktop.
rust message ring
This file contains hidden or 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::{Chan, Port, stream}; | |
| fn main() { | |
| let mut n = 100; | |
| let m = 1000000; | |
| let mut (firstChan, leftPort): (Chan<int>, Port<int>) = stream(); | |
| firstChan.send(m); | |
| let mut (rightChan, rightPort): (Chan<int>, Port<int>) = stream(); | |
| startNode(leftPort, rightChan); | |
| loop { | |
| leftPort <- rightPort; | |
| let (newChan, newPort): (Chan<int>, Port<int>) = stream(); | |
| rightChan <- newChan; | |
| rightPort <- newPort; | |
| startNode(leftPort, rightChan); | |
| n -= 1; | |
| if n <= 2 { | |
| break | |
| } | |
| } | |
| startNode(rightPort, firstChan); | |
| } | |
| fn startNode(port: Port<int>, chan: Chan<int>) { | |
| do spawn { | |
| loop { | |
| let rec = port.recv(); | |
| io::println(fmt!("%d", rec)); | |
| if rec == 0 { | |
| chan.send(0); | |
| break | |
| } | |
| chan.send(rec - 1); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment