Last active
November 10, 2016 02:33
-
-
Save foriequal0/f2c71b86d89160d64ed0ca65bbf89fa6 to your computer and use it in GitHub Desktop.
Warm heaters for winter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rand; | |
use rand::Rng; | |
use std::thread; | |
use std::sync::{Arc}; | |
use std::sync::atomic::{AtomicIsize, Ordering}; | |
use std::iter::FromIterator; | |
fn main() { | |
println!("Heater turned on!"); | |
let degree_of_warmness = 8; | |
let fairly_large: usize = 1024*1024*128; | |
let chunk_shared: Vec<_> = Arc::new((0..fairly_large).map(|_| AtomicIsize::new(0 as isize)).collect()); | |
println!("Fuel loaded!"); | |
let threads:Vec<_> = (0..degree_of_warmness) | |
.map(|id| { | |
let chunk = chunk_shared.clone(); | |
thread::Builder::new() | |
.name(format!("heater_thread {}", id)) | |
.spawn(move || { | |
println!("heater {} is running!", id); | |
let mut rng = rand::IsaacRng::new_unseeded(); | |
while true { | |
let idx:usize = rng.gen_range(0, fairly_large); | |
let some_random:isize = rng.gen_range(-10, 10); | |
let loaded = chunk[idx].load(Ordering::Acquire); | |
chunk[idx].store(loaded + some_random, Ordering::Release); | |
} | |
}) | |
}) | |
.filter(|thread_result| thread_result.is_ok()) | |
.map(|thread_result| thread_result.unwrap()) | |
.collect(); | |
for thread in threads { | |
thread.join().unwrap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment