2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2018-09-25 16:51:35 -05:00
|
|
|
#![allow(unused_imports)]
|
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,
|
2015-08-11 19:27:05 -05:00
|
|
|
I::Item: Send,
|
|
|
|
F: Fn(I::Item) + Sync
|
2015-02-13 23:05:15 -06:00
|
|
|
{
|
2015-08-11 19:27:05 -05:00
|
|
|
for item in iter {
|
|
|
|
f(item)
|
|
|
|
}
|
2015-02-13 23:05:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|