finished. If we didn't want this behaviour, we could use `thread::spawn()`:
```
-# #![feature(old_io, std_misc)]
use std::thread;
-use std::old_io::timer;
-use std::time::Duration;
fn main() {
thread::spawn(|| {
println!("Hello from a thread!");
});
- timer::sleep(Duration::milliseconds(50));
+ thread::sleep_ms(50);
}
```
languages. It will not compile:
```ignore
-# #![feature(old_io, std_misc)]
use std::thread;
-use std::old_io::timer;
-use std::time::Duration;
fn main() {
let mut data = vec![1u32, 2, 3];
});
}
- timer::sleep(Duration::milliseconds(50));
+ thread::sleep_ms(50);
}
```
This gives us an error:
```text
-12:17 error: capture of moved value: `data`
+8:17 error: capture of moved value: `data`
data[i] += 1;
^~~~
```
but for a different reason:
```ignore
-# #![feature(old_io, std_misc)]
use std::thread;
-use std::old_io::timer;
-use std::time::Duration;
use std::sync::Mutex;
fn main() {
});
}
- timer::sleep(Duration::milliseconds(50));
+ thread::sleep_ms(50);
}
```
Here's the error:
```text
-<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]
+<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]
<anon>:11 thread::spawn(move || {
^~~~~~~~~~~~~
-<anon>:11:9: 11:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` cannot be sent between threads safely
+<anon>:9:9: 9:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` cannot be sent between threads safely
<anon>:11 thread::spawn(move || {
^~~~~~~~~~~~~
```
We can use `Arc<T>` to fix this. Here's the working version:
```
-# #![feature(old_io, 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]));
});
}
- timer::sleep(Duration::milliseconds(50));
+ thread::sleep_ms(50);
}
```
thread more closely:
```
-# #![feature(old_io, 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]));
# for i in 0..2 {
data[i] += 1;
});
# }
+#
+# thread::sleep_ms(50);
# }
```