Skip to content

Commit 8af4750

Browse files
committed
fix shootout-threadring.rs
Without joining the threads, the program can finish before the end of the trip of the token.
1 parent a09b139 commit 8af4750

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/test/bench/shootout-threadring.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@
4141
use std::sync::mpsc::{channel, Sender, Receiver};
4242
use std::thread::Thread;
4343

44-
fn start(n_tasks: int, token: int) {
44+
fn start(n_tasks: i32, token: i32) {
4545
let (tx, mut rx) = channel();
4646
tx.send(token).unwrap();
47-
for i in range(2, n_tasks + 1) {
47+
let mut guards = Vec::with_capacity(n_tasks as usize);
48+
for i in 2 .. n_tasks + 1 {
4849
let (tx, next_rx) = channel();
49-
Thread::spawn(move|| roundtrip(i, tx, rx));
50-
rx = next_rx;
50+
let cur_rx = std::mem::replace(&mut rx, next_rx);
51+
guards.push(Thread::scoped(move|| roundtrip(i, tx, cur_rx)));
5152
}
52-
Thread::spawn(move|| roundtrip(1, tx, rx));
53+
let guard = Thread::scoped(move|| roundtrip(1, tx, rx));
5354
}
5455

55-
fn roundtrip(id: int, tx: Sender<int>, rx: Receiver<int>) {
56+
fn roundtrip(id: i32, tx: Sender<i32>, rx: Receiver<i32>) {
5657
for token in rx.iter() {
5758
if token == 1 {
5859
println!("{}", id);
@@ -64,7 +65,6 @@ fn roundtrip(id: int, tx: Sender<int>, rx: Receiver<int>) {
6465

6566
fn main() {
6667
let args = std::os::args();
67-
let args = args.as_slice();
6868
let token = if std::os::getenv("RUST_BENCH").is_some() {
6969
2000000
7070
} else {

0 commit comments

Comments
 (0)