2015-02-13 23:05:15 -06:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2015-04-13 17:15:32 -05:00
|
|
|
#![feature(core, std_misc, scoped)]
|
2015-03-30 13:00:05 -05:00
|
|
|
use std::thread;
|
2015-02-13 23:05:15 -06:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
fn par_for<I, F>(iter: I, f: F)
|
|
|
|
where I: Iterator,
|
|
|
|
<I as Iterator>::Item: Send,
|
|
|
|
F: Fn(<I as Iterator>::Item) + Sync
|
|
|
|
{
|
|
|
|
let f = &f;
|
|
|
|
let _guards: Vec<_> = iter.map(|elem| {
|
2015-03-30 13:00:05 -05:00
|
|
|
thread::scoped(move || {
|
2015-02-13 23:05:15 -06:00
|
|
|
f(elem)
|
|
|
|
})
|
|
|
|
}).collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sum(x: &[i32]) {
|
|
|
|
let sum_lengths = Mutex::new(0);
|
|
|
|
par_for(x.windows(4), |x| {
|
|
|
|
*sum_lengths.lock().unwrap() += x.len()
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(*sum_lengths.lock().unwrap(), (x.len() - 3) * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut elements = [0; 20];
|
|
|
|
|
|
|
|
// iterators over references into this stack frame
|
|
|
|
par_for(elements.iter_mut().enumerate(), |(i, x)| {
|
|
|
|
*x = i as i32
|
|
|
|
});
|
|
|
|
|
|
|
|
sum(&elements)
|
|
|
|
}
|