]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/task-comm-14.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / task-comm-14.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
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
11 #![feature(std_misc)]
12
13 use std::sync::mpsc::{channel, Sender};
14 use std::thread;
15
16 pub fn main() {
17     let (tx, rx) = channel();
18
19     // Spawn 10 threads each sending us back one isize.
20     let mut i = 10;
21     while (i > 0) {
22         println!("{}", i);
23         let tx = tx.clone();
24         thread::spawn({let i = i; move|| { child(i, &tx) }});
25         i = i - 1;
26     }
27
28     // Spawned threads are likely killed before they get a chance to send
29     // anything back, so we deadlock here.
30
31     i = 10;
32     while (i > 0) {
33         println!("{}", i);
34         rx.recv().unwrap();
35         i = i - 1;
36     }
37
38     println!("main thread exiting");
39 }
40
41 fn child(x: isize, tx: &Sender<isize>) {
42     println!("{}", x);
43     tx.send(x).unwrap();
44 }