rust/src/test/run-pass/task-comm-3.rs
Alex Crichton d4a2c94180 std: Clean out #[deprecated] APIs
This commit cleans out a large amount of deprecated APIs from the standard
library and some of the facade crates as well, updating all users in the
compiler and in tests as it goes along.
2015-03-31 15:49:57 -07:00

74 lines
1.9 KiB
Rust

// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(std_misc)]
// no-pretty-expanded FIXME #15189
use std::thread;
use std::sync::mpsc::{channel, Sender};
pub fn main() { println!("===== WITHOUT THREADS ====="); test00(); }
fn test00_start(ch: &Sender<isize>, message: isize, count: isize) {
println!("Starting test00_start");
let mut i: isize = 0;
while i < count {
println!("Sending Message");
ch.send(message + 0).unwrap();
i = i + 1;
}
println!("Ending test00_start");
}
fn test00() {
let number_of_tasks: isize = 16;
let number_of_messages: isize = 4;
println!("Creating tasks");
let (tx, rx) = channel();
let mut i: isize = 0;
// Create and spawn tasks...
let mut results = Vec::new();
while i < number_of_tasks {
let tx = tx.clone();
results.push(thread::scoped({
let i = i;
move|| {
test00_start(&tx, i, number_of_messages)
}
}));
i = i + 1;
}
// Read from spawned tasks...
let mut sum = 0;
for _r in &results {
i = 0;
while i < number_of_messages {
let value = rx.recv().unwrap();
sum += value;
i = i + 1;
}
}
// Join spawned tasks...
for r in results { r.join(); }
println!("Completed: Final number is: ");
println!("{}", sum);
// assert (sum == (((number_of_tasks * (number_of_tasks - 1)) / 2) *
// number_of_messages));
assert_eq!(sum, 480);
}