From 188313d52087c9c9c34f5fd1fc721c19e78efbdc Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Wed, 16 May 2018 15:44:47 -0600 Subject: [PATCH] thread 5 --- Chapter08/Cargo.toml | 6 ++++++ Chapter08/thread5.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Chapter08/thread5.rs diff --git a/Chapter08/Cargo.toml b/Chapter08/Cargo.toml index d9329cc..122ea44 100644 --- a/Chapter08/Cargo.toml +++ b/Chapter08/Cargo.toml @@ -5,6 +5,8 @@ version = "1.0.0" [dependencies] nix = "0.10" thread-id = "3.3" +lazy_static = "1.0" +rand = "0.4.2" [[bin]] name = "process_a" @@ -41,3 +43,7 @@ path = "thread3.rs" [[bin]] name = "thread4" path = "thread4.rs" + +[[bin]] +name = "thread5" +path = "thread5.rs" diff --git a/Chapter08/thread5.rs b/Chapter08/thread5.rs new file mode 100644 index 0000000..3384b02 --- /dev/null +++ b/Chapter08/thread5.rs @@ -0,0 +1,46 @@ +use std::{thread,time}; +extern crate rand; +use std::sync::{Arc,Mutex}; +#[macro_use] extern crate lazy_static; + +lazy_static! { + static ref NEURAL_NET_WEIGHTS: Vec>>> = { + let mut nn = Vec::with_capacity(10000); + for _ in 0..10000 { + let mut mm = Vec::with_capacity(100); + for _ in 0..100 { + mm.push(rand::random::()); + } + let mm = Arc::new(Mutex::new(mm)); + nn.push(mm); + } + nn + }; +} + +fn train() { + let t = time::Duration::from_millis(100); + loop { + for _ in 0..100 { + let update_position = rand::random::() % 1000000; + let update_column = update_position / 10000; + let update_row = update_position % 100; + let update_value = rand::random::(); + let mut update_column = NEURAL_NET_WEIGHTS[update_column as usize].lock().unwrap(); + update_column[update_row as usize] = update_value; + } + thread::sleep(t); + } +} + +fn main() { + let t = time::Duration::from_millis(1000); + + for _ in 0..500 { + thread::spawn(train); + } + + loop { + thread::sleep(t); + } +}