@@ -57,13 +57,13 @@ place!
5757## Threads
5858
5959Rust's standard library provides a library for 'threads', which allow you to
60- run Rust code in parallel. Here's a basic example of using ` Thread ` :
60+ run Rust code in parallel. Here's a basic example of using ` std::thread ` :
6161
6262```
63- use std::thread::Thread ;
63+ use std::thread;
6464
6565fn main() {
66- Thread ::scoped(|| {
66+ thread ::scoped(|| {
6767 println!("Hello from a thread!");
6868 });
6969}
@@ -73,10 +73,10 @@ The `Thread::scoped()` method accepts a closure, which is executed in a new
7373thread. It's called ` scoped ` because this thread returns a join guard:
7474
7575```
76- use std::thread::Thread ;
76+ use std::thread;
7777
7878fn main() {
79- let guard = Thread ::scoped(|| {
79+ let guard = thread ::scoped(|| {
8080 println!("Hello from a thread!");
8181 });
8282
@@ -85,40 +85,22 @@ fn main() {
8585```
8686
8787When ` guard ` goes out of scope, it will block execution until the thread is
88- finished. If we didn't want this behaviour, we could use ` Thread ::spawn()` :
88+ finished. If we didn't want this behaviour, we could use ` thread ::spawn()` :
8989
9090```
91- use std::thread::Thread ;
91+ use std::thread;
9292use std::old_io::timer;
9393use std::time::Duration;
9494
9595fn main() {
96- Thread ::spawn(|| {
96+ thread ::spawn(|| {
9797 println!("Hello from a thread!");
9898 });
9999
100100 timer::sleep(Duration::milliseconds(50));
101101}
102102```
103103
104- Or call ` .detach() ` :
105-
106- ```
107- use std::thread::Thread;
108- use std::old_io::timer;
109- use std::time::Duration;
110-
111- fn main() {
112- let guard = Thread::scoped(|| {
113- println!("Hello from a thread!");
114- });
115-
116- guard.detach();
117-
118- timer::sleep(Duration::milliseconds(50));
119- }
120- ```
121-
122104We need to ` sleep ` here because when ` main() ` ends, it kills all of the
123105running threads.
124106
@@ -164,15 +146,15 @@ As an example, here is a Rust program that would have a data race in many
164146languages. It will not compile:
165147
166148``` ignore
167- use std::thread::Thread ;
149+ use std::thread;
168150use std::old_io::timer;
169151use std::time::Duration;
170152
171153fn main() {
172154 let mut data = vec![1u32, 2, 3];
173155
174156 for i in 0..2 {
175- Thread ::spawn(move || {
157+ thread ::spawn(move || {
176158 data[i] += 1;
177159 });
178160 }
@@ -203,7 +185,7 @@ only one person at a time can mutate what's inside. For that, we can use the
203185but for a different reason:
204186
205187``` ignore
206- use std::thread::Thread ;
188+ use std::thread;
207189use std::old_io::timer;
208190use std::time::Duration;
209191use std::sync::Mutex;
@@ -213,7 +195,7 @@ fn main() {
213195
214196 for i in 0..2 {
215197 let data = data.lock().unwrap();
216- Thread ::spawn(move || {
198+ thread ::spawn(move || {
217199 data[i] += 1;
218200 });
219201 }
@@ -255,7 +237,7 @@ We can use `Arc<T>` to fix this. Here's the working version:
255237
256238```
257239use std::sync::{Arc, Mutex};
258- use std::thread::Thread ;
240+ use std::thread;
259241use std::old_io::timer;
260242use std::time::Duration;
261243
@@ -264,7 +246,7 @@ fn main() {
264246
265247 for i in 0us..2 {
266248 let data = data.clone();
267- Thread ::spawn(move || {
249+ thread ::spawn(move || {
268250 let mut data = data.lock().unwrap();
269251 data[i] += 1;
270252 });
@@ -280,14 +262,14 @@ thread more closely:
280262
281263```
282264# use std::sync::{Arc, Mutex};
283- # use std::thread::Thread ;
265+ # use std::thread;
284266# use std::old_io::timer;
285267# use std::time::Duration;
286268# fn main() {
287269# let data = Arc::new(Mutex::new(vec![1u32, 2, 3]));
288270# for i in 0us..2 {
289271# let data = data.clone();
290- Thread ::spawn(move || {
272+ thread ::spawn(move || {
291273 let mut data = data.lock().unwrap();
292274 data[i] += 1;
293275});
@@ -315,7 +297,7 @@ than waiting for a specific time:
315297
316298```
317299use std::sync::{Arc, Mutex};
318- use std::thread::Thread ;
300+ use std::thread;
319301use std::sync::mpsc;
320302
321303fn main() {
@@ -326,7 +308,7 @@ fn main() {
326308 for _ in 0..10 {
327309 let (data, tx) = (data.clone(), tx.clone());
328310
329- Thread ::spawn(move || {
311+ thread ::spawn(move || {
330312 let mut data = data.lock().unwrap();
331313 *data += 1;
332314
@@ -348,7 +330,7 @@ is `Send` over the channel!
348330
349331```
350332use std::sync::{Arc, Mutex};
351- use std::thread::Thread ;
333+ use std::thread;
352334use std::sync::mpsc;
353335
354336fn main() {
@@ -357,7 +339,7 @@ fn main() {
357339 for _ in 0..10 {
358340 let tx = tx.clone();
359341
360- Thread ::spawn(move || {
342+ thread ::spawn(move || {
361343 let answer = 42u32;
362344
363345 tx.send(answer);
@@ -378,9 +360,9 @@ A `panic!` will crash the currently executing thread. You can use Rust's
378360threads as a simple isolation mechanism:
379361
380362```
381- use std::thread::Thread ;
363+ use std::thread;
382364
383- let result = Thread::scoped (move || {
365+ let result = thread::spawn (move || {
384366 panic!("oops!");
385367}).join();
386368
0 commit comments