]> git.lizzy.rs Git - rust.git/blob - src/test/ui/threads-sendsync/task-comm-14.rs
Rollup merge of #62600 - emmericp:libtest-add-show-output, r=gnzlbg
[rust.git] / src / test / ui / threads-sendsync / task-comm-14.rs
1 // run-pass
2 #![allow(unused_parens)]
3 // ignore-emscripten no threads support
4
5 use std::sync::mpsc::{channel, Sender};
6 use std::thread;
7
8 pub fn main() {
9     let (tx, rx) = channel();
10
11     // Spawn 10 threads each sending us back one isize.
12     let mut i = 10;
13     while (i > 0) {
14         println!("{}", i);
15         let tx = tx.clone();
16         thread::spawn({let i = i; move|| { child(i, &tx) }});
17         i = i - 1;
18     }
19
20     // Spawned threads are likely killed before they get a chance to send
21     // anything back, so we deadlock here.
22
23     i = 10;
24     while (i > 0) {
25         println!("{}", i);
26         rx.recv().unwrap();
27         i = i - 1;
28     }
29
30     println!("main thread exiting");
31 }
32
33 fn child(x: isize, tx: &Sender<isize>) {
34     println!("{}", x);
35     tx.send(x).unwrap();
36 }