41
41
use std:: sync:: mpsc:: { channel, Sender , Receiver } ;
42
42
use std:: thread:: Thread ;
43
43
44
- fn start ( n_tasks : int , token : int ) {
44
+ fn start ( n_tasks : i32 , token : i32 ) {
45
45
let ( tx, mut rx) = channel ( ) ;
46
46
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 {
48
49
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 ) ) ) ;
51
52
}
52
- Thread :: spawn ( move || roundtrip ( 1 , tx, rx) ) ;
53
+ let guard = Thread :: scoped ( move || roundtrip ( 1 , tx, rx) ) ;
53
54
}
54
55
55
- fn roundtrip ( id : int , tx : Sender < int > , rx : Receiver < int > ) {
56
+ fn roundtrip ( id : i32 , tx : Sender < i32 > , rx : Receiver < i32 > ) {
56
57
for token in rx. iter ( ) {
57
58
if token == 1 {
58
59
println ! ( "{}" , id) ;
@@ -64,7 +65,6 @@ fn roundtrip(id: int, tx: Sender<int>, rx: Receiver<int>) {
64
65
65
66
fn main ( ) {
66
67
let args = std:: os:: args ( ) ;
67
- let args = args. as_slice ( ) ;
68
68
let token = if std:: os:: getenv ( "RUST_BENCH" ) . is_some ( ) {
69
69
2000000
70
70
} else {
0 commit comments