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