From 0cef4533e78dadd05feafd3392990fae2d2168b4 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Tue, 31 Mar 2015 01:04:31 +0200 Subject: [PATCH 1/2] book: replace the deprecated sleep function --- src/doc/trpl/concurrency.md | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/doc/trpl/concurrency.md b/src/doc/trpl/concurrency.md index 79cb3117c0ef2..72a9314490131 100644 --- a/src/doc/trpl/concurrency.md +++ b/src/doc/trpl/concurrency.md @@ -88,9 +88,8 @@ When `guard` goes out of scope, it will block execution until the thread is finished. If we didn't want this behaviour, we could use `thread::spawn()`: ``` -# #![feature(old_io, std_misc)] +# #![feature(thread_sleep, std_misc)] use std::thread; -use std::old_io::timer; use std::time::Duration; fn main() { @@ -98,7 +97,7 @@ fn main() { println!("Hello from a thread!"); }); - timer::sleep(Duration::milliseconds(50)); + thread::sleep(Duration::milliseconds(50)); } ``` @@ -147,9 +146,8 @@ As an example, here is a Rust program that would have a data race in many languages. It will not compile: ```ignore -# #![feature(old_io, std_misc)] +# #![feature(thread_sleep, std_misc)] use std::thread; -use std::old_io::timer; use std::time::Duration; fn main() { @@ -161,7 +159,7 @@ fn main() { }); } - timer::sleep(Duration::milliseconds(50)); + thread::sleep(Duration::milliseconds(50)); } ``` @@ -187,9 +185,8 @@ only one person at a time can mutate what's inside. For that, we can use the but for a different reason: ```ignore -# #![feature(old_io, std_misc)] +# #![feature(thread_sleep, std_misc)] use std::thread; -use std::old_io::timer; use std::time::Duration; use std::sync::Mutex; @@ -203,7 +200,7 @@ fn main() { }); } - timer::sleep(Duration::milliseconds(50)); + thread::sleep(Duration::milliseconds(50)); } ``` @@ -232,10 +229,9 @@ guard across thread boundaries, which gives us our error. We can use `Arc` to fix this. Here's the working version: ``` -# #![feature(old_io, std_misc)] +# #![feature(thread_sleep, std_misc)] use std::sync::{Arc, Mutex}; use std::thread; -use std::old_io::timer; use std::time::Duration; fn main() { @@ -249,7 +245,7 @@ fn main() { }); } - timer::sleep(Duration::milliseconds(50)); + thread::sleep(Duration::milliseconds(50)); } ``` @@ -258,10 +254,9 @@ handle is then moved into the new thread. Let's examine the body of the thread more closely: ``` -# #![feature(old_io, std_misc)] +# #![feature(std_misc)] # use std::sync::{Arc, Mutex}; # use std::thread; -# use std::old_io::timer; # use std::time::Duration; # fn main() { # let data = Arc::new(Mutex::new(vec![1u32, 2, 3])); From 8e04b1ad5a2022775a84105a8d82964c9df08a37 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Fri, 3 Apr 2015 22:01:09 +0200 Subject: [PATCH 2/2] book: update code from preceding commit --- src/doc/trpl/concurrency.md | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/doc/trpl/concurrency.md b/src/doc/trpl/concurrency.md index 72a9314490131..a7c6c6401fbd5 100644 --- a/src/doc/trpl/concurrency.md +++ b/src/doc/trpl/concurrency.md @@ -88,16 +88,14 @@ When `guard` goes out of scope, it will block execution until the thread is finished. If we didn't want this behaviour, we could use `thread::spawn()`: ``` -# #![feature(thread_sleep, std_misc)] use std::thread; -use std::time::Duration; fn main() { thread::spawn(|| { println!("Hello from a thread!"); }); - thread::sleep(Duration::milliseconds(50)); + thread::sleep_ms(50); } ``` @@ -146,9 +144,7 @@ As an example, here is a Rust program that would have a data race in many languages. It will not compile: ```ignore -# #![feature(thread_sleep, std_misc)] use std::thread; -use std::time::Duration; fn main() { let mut data = vec![1u32, 2, 3]; @@ -159,16 +155,16 @@ fn main() { }); } - thread::sleep(Duration::milliseconds(50)); + thread::sleep_ms(50); } ``` This gives us an error: ```text -12:17 error: capture of moved value: `data` - data[i] += 1; - ^~~~ +error: capture of moved value: `data` + data[i] += 1; + ^~~~ ``` In this case, we know that our code _should_ be safe, but Rust isn't sure. And @@ -185,9 +181,7 @@ only one person at a time can mutate what's inside. For that, we can use the but for a different reason: ```ignore -# #![feature(thread_sleep, std_misc)] use std::thread; -use std::time::Duration; use std::sync::Mutex; fn main() { @@ -200,19 +194,19 @@ fn main() { }); } - thread::sleep(Duration::milliseconds(50)); + thread::sleep_ms(50); } ``` Here's the error: ```text -:11:9: 11:22 error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec>` [E0277] -:11 thread::spawn(move || { - ^~~~~~~~~~~~~ -:11:9: 11:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec>` cannot be sent between threads safely -:11 thread::spawn(move || { - ^~~~~~~~~~~~~ +:9:9: 9:22 error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec>` [E0277] +:9 thread::spawn(move || { + ^~~~~~~~~~~~~ +:9:9: 9:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec>` cannot be sent between threads safely +:9 thread::spawn(move || { + ^~~~~~~~~~~~~ ``` You see, [`Mutex`](std/sync/struct.Mutex.html) has a @@ -229,10 +223,8 @@ guard across thread boundaries, which gives us our error. We can use `Arc` to fix this. Here's the working version: ``` -# #![feature(thread_sleep, std_misc)] use std::sync::{Arc, Mutex}; use std::thread; -use std::time::Duration; fn main() { let data = Arc::new(Mutex::new(vec![1u32, 2, 3])); @@ -245,7 +237,7 @@ fn main() { }); } - thread::sleep(Duration::milliseconds(50)); + thread::sleep_ms(50); } ```