]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/task-comm-7.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / test / run-pass / task-comm-7.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 // ignore-emscripten no threads support
12
13 #![allow(dead_assignment)]
14
15 use std::sync::mpsc::{channel, Sender};
16 use std::thread;
17
18 pub fn main() { test00(); }
19
20 fn test00_start(c: &Sender<isize>, start: isize,
21                 number_of_messages: isize) {
22     let mut i: isize = 0;
23     while i < number_of_messages { c.send(start + i).unwrap(); i += 1; }
24 }
25
26 fn test00() {
27     let mut r: isize = 0;
28     let mut sum: isize = 0;
29     let (tx, rx) = channel();
30     let number_of_messages: isize = 10;
31
32     let tx2 = tx.clone();
33     let t1 = thread::spawn(move|| {
34         test00_start(&tx2, number_of_messages * 0, number_of_messages);
35     });
36     let tx2 = tx.clone();
37     let t2 = thread::spawn(move|| {
38         test00_start(&tx2, number_of_messages * 1, number_of_messages);
39     });
40     let tx2 = tx.clone();
41     let t3 = thread::spawn(move|| {
42         test00_start(&tx2, number_of_messages * 2, number_of_messages);
43     });
44     let tx2 = tx.clone();
45     let t4 = thread::spawn(move|| {
46         test00_start(&tx2, number_of_messages * 3, number_of_messages);
47     });
48
49     let mut i: isize = 0;
50     while i < number_of_messages {
51         r = rx.recv().unwrap();
52         sum += r;
53         r = rx.recv().unwrap();
54         sum += r;
55         r = rx.recv().unwrap();
56         sum += r;
57         r = rx.recv().unwrap();
58         sum += r;
59         i += 1;
60     }
61
62     assert_eq!(sum, number_of_messages * 4 * (number_of_messages * 4 - 1) / 2);
63
64     t1.join();
65     t2.join();
66     t3.join();
67     t4.join();
68 }