]>
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 | ||
c34b1796 AL |
11 | #![feature(std_misc)] |
12 | ||
1a4d82fc | 13 | // no-pretty-expanded FIXME #15189 |
223e47cc | 14 | |
c34b1796 | 15 | use std::thread; |
1a4d82fc | 16 | use std::sync::mpsc::{channel, Sender}; |
970d7e83 | 17 | |
1a4d82fc | 18 | pub fn main() { println!("===== WITHOUT THREADS ====="); test00(); } |
223e47cc | 19 | |
c34b1796 | 20 | fn test00_start(ch: &Sender<isize>, message: isize, count: isize) { |
1a4d82fc | 21 | println!("Starting test00_start"); |
c34b1796 | 22 | let mut i: isize = 0; |
223e47cc | 23 | while i < count { |
1a4d82fc JJ |
24 | println!("Sending Message"); |
25 | ch.send(message + 0).unwrap(); | |
223e47cc LB |
26 | i = i + 1; |
27 | } | |
1a4d82fc | 28 | println!("Ending test00_start"); |
223e47cc LB |
29 | } |
30 | ||
31 | fn test00() { | |
c34b1796 AL |
32 | let number_of_tasks: isize = 16; |
33 | let number_of_messages: isize = 4; | |
223e47cc | 34 | |
1a4d82fc | 35 | println!("Creating tasks"); |
223e47cc | 36 | |
1a4d82fc | 37 | let (tx, rx) = channel(); |
223e47cc | 38 | |
c34b1796 | 39 | let mut i: isize = 0; |
223e47cc LB |
40 | |
41 | // Create and spawn tasks... | |
1a4d82fc | 42 | let mut results = Vec::new(); |
223e47cc | 43 | while i < number_of_tasks { |
1a4d82fc | 44 | let tx = tx.clone(); |
c34b1796 | 45 | results.push(thread::scoped({ |
223e47cc | 46 | let i = i; |
1a4d82fc JJ |
47 | move|| { |
48 | test00_start(&tx, i, number_of_messages) | |
49 | } | |
50 | })); | |
223e47cc LB |
51 | i = i + 1; |
52 | } | |
53 | ||
54 | // Read from spawned tasks... | |
55 | let mut sum = 0; | |
85aaf69f | 56 | for _r in &results { |
223e47cc LB |
57 | i = 0; |
58 | while i < number_of_messages { | |
1a4d82fc | 59 | let value = rx.recv().unwrap(); |
223e47cc LB |
60 | sum += value; |
61 | i = i + 1; | |
62 | } | |
63 | } | |
64 | ||
65 | // Join spawned tasks... | |
85aaf69f | 66 | for r in results { r.join(); } |
223e47cc | 67 | |
1a4d82fc JJ |
68 | println!("Completed: Final number is: "); |
69 | println!("{}", sum); | |
223e47cc LB |
70 | // assert (sum == (((number_of_tasks * (number_of_tasks - 1)) / 2) * |
71 | // number_of_messages)); | |
970d7e83 | 72 | assert_eq!(sum, 480); |
223e47cc | 73 | } |