Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

50 lines (44 sloc) 1.15 kb
// Based on threadring.erlang by Jira Isa
use std;
const n_threads: int = 503;
fn start(+token: int) {
import iter::*;
let p = comm::port();
let ch = iter::foldl(bind int::range(2, n_threads + 1, _),
comm::chan(p)) { |ch, i|
// FIXME: Some twiddling because we don't have a standard
// reverse range function yet
let id = n_threads + 2 - i;
let {to_child, _} = task::spawn_connected::<int, int> {|p, _ch|
roundtrip(id, p, ch)
};
to_child
};
comm::send(ch, token);
roundtrip(1, p, ch);
}
fn roundtrip(id: int, p: comm::port<int>, ch: comm::chan<int>) {
while (true) {
alt comm::recv(p) {
1 {
std::io::println(#fmt("%d\n", id));
ret;
}
token {
#debug("%d %d", id, token);
comm::send(ch, token - 1);
if token <= n_threads {
ret;
}
}
}
}
}
fn main(args: [str]) {
let token = if vec::len(args) < 2u {
1000
} else {
int::from_str(args[1])
};
start(token);
}
Jump to Line
Something went wrong with that request. Please try again.