changeset 8:2c6285996268

pub fn worked
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 12 Jan 2021 14:56:07 +0900
parents 8768d36c3b69
children aaba40049c28
files src/t16thread/src/lib.rs src/t16thread/src/main.rs src/t16thread/src/mpsc_test.rs src/t16thread/src/racecondition.rs
diffstat 4 files changed, 73 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/src/t16thread/src/lib.rs	Tue Jan 12 14:34:05 2021 +0900
+++ b/src/t16thread/src/lib.rs	Tue Jan 12 14:56:07 2021 +0900
@@ -2,6 +2,7 @@
   t16 thread
 */
 
-#![deny(missing_docs)]
+// #![deny(missing_docs)]
 
-mod racecondition;
+pub mod racecondition;
+pub mod mpsc_test;
--- a/src/t16thread/src/main.rs	Tue Jan 12 14:34:05 2021 +0900
+++ b/src/t16thread/src/main.rs	Tue Jan 12 14:56:07 2021 +0900
@@ -1,6 +1,6 @@
-mod mpsc_test;
-// use racecondition;
-use racecondition::mainc;
+use t16thread::mpsc_test::mainc;
+use t16thread::mpsc_test::mainm;
+use t16thread::racecondition::mainr;
 
 use std::thread;
 use std::time::Duration;
@@ -29,7 +29,7 @@
     }
     handle.join().unwrap();  // without this, some data are dropped
     // main1();
-    // mpsc_test::main_mpsc();
-    // mpsc_test::mainc();
-    racecondition::mainr();
+    t16thread::mpsc_test::mainm();
+    t16thread::mpsc_test::mainc();
+    t16thread::racecondition::mainr();
 }
\ No newline at end of file
--- a/src/t16thread/src/mpsc_test.rs	Tue Jan 12 14:34:05 2021 +0900
+++ b/src/t16thread/src/mpsc_test.rs	Tue Jan 12 14:56:07 2021 +0900
@@ -1,65 +1,64 @@
-
-pub mod mpsc_test {
-    use std::sync::mpsc;
-    use std::thread;
-    use std::time::Duration;
-    pub fn main_mpsc() {
-        let (tx, rx) = mpsc::channel();
-
-        thread::spawn(move || {
-            let vals = vec![
-                String::from("hi"),
-                String::from("from"),
-                String::from("the"),
-                String::from("thread"),
-            ];
-
-            for val in vals {
-                tx.send(val).unwrap();
-                thread::sleep(Duration::from_secs(1));
-            }
-        });
-
-        for received in rx {
-            println!("Got: {}", received);
-        }
-    }
-
-    pub fn mainc() {
-        let (tx, rx) = mpsc::channel();
+// #![deny(missing_docs)]
+use std::sync::mpsc;
+use std::thread;
+use std::time::Duration;
+pub fn mainm() {
+    let (tx, rx) = mpsc::channel();
 
-        let tx1 = mpsc::Sender::clone(&tx);
-        thread::spawn(move || {
-            let vals = vec![
-                String::from("hi"),
-                String::from("from"),
-                String::from("the"),
-                String::from("thread"),
-            ];
-
-            for val in vals {
-                tx1.send(val).unwrap();
-                thread::sleep(Duration::from_secs(1));
-            }
-        });
+    thread::spawn(move || {
+        let vals = vec![
+            String::from("hi"),
+            String::from("from"),
+            String::from("the"),
+            String::from("thread"),
+        ];
 
-        thread::spawn(move || {
-            let vals = vec![
-                String::from("more"),
-                String::from("messages"),
-                String::from("for"),
-                String::from("you"),
-            ];
+        for val in vals {
+            tx.send(val).unwrap();
+            thread::sleep(Duration::from_secs(1));
+        }
+    });
 
-            for val in vals {
-                tx.send(val).unwrap();
-                thread::sleep(Duration::from_secs(1));
-            }
-        });
-
-        for received in rx {
-            println!("Got: {}", received);
-        }
+    for received in rx {
+        println!("Got: {}", received);
     }
 }
 
+pub fn mainc() {
+    let (tx, rx) = mpsc::channel();
+
+    let tx1 = mpsc::Sender::clone(&tx);
+    thread::spawn(move || {
+        let vals = vec![
+            String::from("hi"),
+            String::from("from"),
+            String::from("the"),
+            String::from("thread"),
+        ];
+
+        for val in vals {
+            tx1.send(val).unwrap();
+            thread::sleep(Duration::from_secs(1));
+        }
+    });
+
+    thread::spawn(move || {
+        let vals = vec![
+            String::from("more"),
+            String::from("messages"),
+            String::from("for"),
+            String::from("you"),
+        ];
+
+        for val in vals {
+            tx.send(val).unwrap();
+            thread::sleep(Duration::from_secs(1));
+        }
+    });
+
+    for received in rx {
+        println!("Got: {}", received);
+    }
+}
+
+
--- a/src/t16thread/src/racecondition.rs	Tue Jan 12 14:34:05 2021 +0900
+++ b/src/t16thread/src/racecondition.rs	Tue Jan 12 14:56:07 2021 +0900
@@ -1,9 +1,13 @@
-use std::sync::Mutex;
+// #![deny(missing_docs)]
+use std::sync::{Arc, Mutex};
 use std::thread;
+
 pub fn mainr() {
-    let counter = Mutex::new(0);
+    let counter = Arc::new(Mutex::new(0));
     let mut handles = vec![];
+
     for _ in 0..10 {
+        let counter = Arc::clone(&counter);
         let handle = thread::spawn(move || {
             let mut num = counter.lock().unwrap();