You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/doc/trpl/concurrency.md
+9-23
Original file line number
Diff line number
Diff line change
@@ -88,17 +88,14 @@ When `guard` goes out of scope, it will block execution until the thread is
88
88
finished. If we didn't want this behaviour, we could use `thread::spawn()`:
89
89
90
90
```
91
-
# #![feature(old_io, std_misc)]
92
91
use std::thread;
93
-
use std::old_io::timer;
94
-
use std::time::Duration;
95
92
96
93
fn main() {
97
94
thread::spawn(|| {
98
95
println!("Hello from a thread!");
99
96
});
100
97
101
-
timer::sleep(Duration::milliseconds(50));
98
+
thread::sleep_ms(50);
102
99
}
103
100
```
104
101
@@ -147,10 +144,7 @@ As an example, here is a Rust program that would have a data race in many
147
144
languages. It will not compile:
148
145
149
146
```ignore
150
-
# #![feature(old_io, std_misc)]
151
147
use std::thread;
152
-
use std::old_io::timer;
153
-
use std::time::Duration;
154
148
155
149
fn main() {
156
150
let mut data = vec![1u32, 2, 3];
@@ -161,14 +155,14 @@ fn main() {
161
155
});
162
156
}
163
157
164
-
timer::sleep(Duration::milliseconds(50));
158
+
thread::sleep_ms(50);
165
159
}
166
160
```
167
161
168
162
This gives us an error:
169
163
170
164
```text
171
-
12:17 error: capture of moved value: `data`
165
+
8:17 error: capture of moved value: `data`
172
166
data[i] += 1;
173
167
^~~~
174
168
```
@@ -187,10 +181,7 @@ only one person at a time can mutate what's inside. For that, we can use the
187
181
but for a different reason:
188
182
189
183
```ignore
190
-
# #![feature(old_io, std_misc)]
191
184
use std::thread;
192
-
use std::old_io::timer;
193
-
use std::time::Duration;
194
185
use std::sync::Mutex;
195
186
196
187
fn main() {
@@ -203,17 +194,17 @@ fn main() {
203
194
});
204
195
}
205
196
206
-
timer::sleep(Duration::milliseconds(50));
197
+
thread::sleep_ms(50);
207
198
}
208
199
```
209
200
210
201
Here's the error:
211
202
212
203
```text
213
-
<anon>:11:9: 11:22 error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` [E0277]
204
+
<anon>:9:9: 9:22 error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` [E0277]
214
205
<anon>:11 thread::spawn(move || {
215
206
^~~~~~~~~~~~~
216
-
<anon>:11:9: 11:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` cannot be sent between threads safely
207
+
<anon>:9:9: 9:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` cannot be sent between threads safely
217
208
<anon>:11 thread::spawn(move || {
218
209
^~~~~~~~~~~~~
219
210
```
@@ -232,11 +223,8 @@ guard across thread boundaries, which gives us our error.
232
223
We can use `Arc<T>` to fix this. Here's the working version:
233
224
234
225
```
235
-
# #![feature(old_io, std_misc)]
236
226
use std::sync::{Arc, Mutex};
237
227
use std::thread;
238
-
use std::old_io::timer;
239
-
use std::time::Duration;
240
228
241
229
fn main() {
242
230
let data = Arc::new(Mutex::new(vec![1u32, 2, 3]));
@@ -249,20 +237,17 @@ fn main() {
249
237
});
250
238
}
251
239
252
-
timer::sleep(Duration::milliseconds(50));
240
+
thread::sleep_ms(50);
253
241
}
254
242
```
255
243
256
244
We now call `clone()` on our `Arc`, which increases the internal count. This
257
245
handle is then moved into the new thread. Let's examine the body of the
0 commit comments