mirror of https://github.com/sunface/rust-course
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
679 B
27 lines
679 B
3 years ago
|
## Arc和Mutex结合实现多线程数据修改和汇总
|
||
|
```rust
|
||
|
use std::sync::{Arc,Mutex};
|
||
|
use std::thread;
|
||
|
use std::time::Duration;
|
||
|
|
||
|
struct JobStatus {
|
||
|
jobs_completed: u32,
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
|
||
|
let status_shared = Arc::clone(&status);
|
||
|
thread::spawn(move || {
|
||
|
for _ in 0..10 {
|
||
|
thread::sleep(Duration::from_millis(250));
|
||
|
let mut s = status_shared.lock().unwrap();
|
||
|
s.jobs_completed += 1;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
while status.lock().unwrap().jobs_completed < 10 {
|
||
|
println!("waiting... ");
|
||
|
thread::sleep(Duration::from_millis(500));
|
||
|
}
|
||
|
}
|
||
|
```
|