]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
b7449926 | 11 | // run-pass |
7453a54e SL |
12 | // ignore-emscripten no threads support |
13 | ||
1a4d82fc | 14 | use std::sync::mpsc::{channel, Sender}; |
c34b1796 | 15 | use std::thread; |
970d7e83 | 16 | |
223e47cc | 17 | pub fn main() { |
1a4d82fc | 18 | let (tx, rx) = channel(); |
223e47cc | 19 | |
bd371182 | 20 | // Spawn 10 threads each sending us back one isize. |
223e47cc LB |
21 | let mut i = 10; |
22 | while (i > 0) { | |
1a4d82fc JJ |
23 | println!("{}", i); |
24 | let tx = tx.clone(); | |
9346a6ac | 25 | thread::spawn({let i = i; move|| { child(i, &tx) }}); |
223e47cc LB |
26 | i = i - 1; |
27 | } | |
28 | ||
bd371182 | 29 | // Spawned threads are likely killed before they get a chance to send |
223e47cc LB |
30 | // anything back, so we deadlock here. |
31 | ||
32 | i = 10; | |
33 | while (i > 0) { | |
1a4d82fc JJ |
34 | println!("{}", i); |
35 | rx.recv().unwrap(); | |
223e47cc LB |
36 | i = i - 1; |
37 | } | |
38 | ||
1a4d82fc | 39 | println!("main thread exiting"); |
223e47cc LB |
40 | } |
41 | ||
c34b1796 | 42 | fn child(x: isize, tx: &Sender<isize>) { |
1a4d82fc JJ |
43 | println!("{}", x); |
44 | tx.send(x).unwrap(); | |
223e47cc | 45 | } |