# HG changeset patch # User Shinji KONO # Date 1610430967 -32400 # Node ID 2c62859962681ebd35b4912707ac6b109fd2ecda # Parent 8768d36c3b6984e5911ff16f5def6c1809a7474a pub fn worked diff -r 8768d36c3b69 -r 2c6285996268 src/t16thread/src/lib.rs --- 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; diff -r 8768d36c3b69 -r 2c6285996268 src/t16thread/src/main.rs --- 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 diff -r 8768d36c3b69 -r 2c6285996268 src/t16thread/src/mpsc_test.rs --- 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); + } +} + + diff -r 8768d36c3b69 -r 2c6285996268 src/t16thread/src/racecondition.rs --- 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();