mirror of
https://github.com/xythrez/RustMP.git
synced 2025-09-04 21:52:36 +00:00
Added critical! macro. Added "private" to par_for. Converted par_for to use tail-recursive parsing.
This commit is contained in:
parent
24c3772926
commit
d7745fd38a
2 changed files with 125 additions and 39 deletions
|
@ -1,5 +1,5 @@
|
|||
use rand::Rng;
|
||||
use rustmp::par_for;
|
||||
use rustmp::{par_for, critical};
|
||||
use std::time;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -11,9 +11,11 @@ struct Student {
|
|||
|
||||
impl Student {
|
||||
pub fn new(age: u8) -> Student {
|
||||
Student { name: "Default".to_string(),
|
||||
age,
|
||||
gpa: age as f32 }
|
||||
Student {
|
||||
name: "Default".to_string(),
|
||||
age,
|
||||
gpa: age as f32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,23 +23,32 @@ fn main() {
|
|||
let numbers: Vec<Student> = vec![];
|
||||
|
||||
par_for! {
|
||||
for i in 1..32, capturing numbers {
|
||||
//std::thread::sleep(
|
||||
// time::Duration::from_secs(
|
||||
// rand::thread_rng().gen_range(1..10)));
|
||||
let mut lock = numbers.write();
|
||||
lock.push(Student::new(i));
|
||||
println!("Thread {} running!", i);
|
||||
} };
|
||||
|
||||
par_for! {
|
||||
for i in 1..32, blocksize 16, capturing numbers {
|
||||
let mut lock = numbers.write();
|
||||
lock.push(Student::new(i));
|
||||
println!("Thread {} running!", i);
|
||||
} };
|
||||
for i in 1..32, blocksize 4, capturing numbers, {
|
||||
//std::thread::sleep(
|
||||
// time::Duration::from_secs(
|
||||
// rand::thread_rng().gen_range(1..10)));
|
||||
critical! {
|
||||
// Automatically locks numbers as read+write,
|
||||
// and makes the result accessible as number
|
||||
readwrite numbers;
|
||||
numbers.push(Student::new(i));
|
||||
}
|
||||
println!("Thread {} running!", i);
|
||||
}
|
||||
}
|
||||
|
||||
for num in numbers {
|
||||
println!("{:?}", num);
|
||||
println!("{:?}", num);
|
||||
}
|
||||
|
||||
//let mut local = 0;
|
||||
//par_for! {
|
||||
//for i in 1..32, blocksize 1, capturing numbers, private local, {
|
||||
//local += 1;
|
||||
//println!("{}", local);
|
||||
//let mut lock = numbers.write();
|
||||
//lock.push(Student::new(i));
|
||||
//println!("Thread {} running!", i);
|
||||
//} }
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue