From f2dad4eb10718cf37d6d825957122f4f81cdb6b5 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 17 May 2018 14:53:53 -0600 Subject: [PATCH] share 7 --- Chapter08/Cargo.toml | 8 ++++++++ Chapter08/share6.rs | 21 +++++++++++++++++++++ Chapter08/share7.rs | 17 +++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 Chapter08/share6.rs create mode 100644 Chapter08/share7.rs diff --git a/Chapter08/Cargo.toml b/Chapter08/Cargo.toml index a77cc08..33ee6c8 100644 --- a/Chapter08/Cargo.toml +++ b/Chapter08/Cargo.toml @@ -68,3 +68,11 @@ path = "share4.rs" name = "share5" path = "share5.rs" +[[bin]] +name = "share6" +path = "share6.rs" + +[[bin]] +name = "share7" +path = "share7.rs" + diff --git a/Chapter08/share6.rs b/Chapter08/share6.rs new file mode 100644 index 0000000..51aaf8a --- /dev/null +++ b/Chapter08/share6.rs @@ -0,0 +1,21 @@ +use std::thread; +use std::sync::{Arc,Mutex}; + +fn main() { + let a = Arc::new(Mutex::new(vec![1, 2, 3])); + { + let a = Arc::clone(&a); + thread::spawn(move || { + let mut a = a.lock().unwrap(); + (*a)[1] = 2; + }); + } + + { + let a = Arc::clone(&a); + thread::spawn(move || { + let mut a = a.lock().unwrap(); + (*a)[1] = 3; + }); + } +} diff --git a/Chapter08/share7.rs b/Chapter08/share7.rs new file mode 100644 index 0000000..80b9760 --- /dev/null +++ b/Chapter08/share7.rs @@ -0,0 +1,17 @@ +use std::thread; + +struct MyBox(u8); +unsafe impl Send for MyBox {} +unsafe impl Sync for MyBox {} + +static A: MyBox = MyBox(22); + +fn main() { + thread::spawn(move || { + A.0 + }); + + thread::spawn(move || { + A.0 + }); +}