Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::thread;
- use std::time::Duration;
- use crossbeam; // 0.6.0
- fn main() {
- let handles = (0..2).map(|worker_id| {
- eprintln!("Spawning worker {}", worker_id);
- let handle = thread::spawn(move || {
- eprintln!("Worker {} is running", worker_id);
- for _ in 0..5 {
- thread::sleep(Duration::from_millis(10));
- eprintln!("Worker {} did some work", worker_id);
- }
- });
- eprintln!("Worker {} has spawned", worker_id);
- handle
- });
- thread::sleep(Duration::from_secs(1));
- eprintln!("Joining workers from the main thread");
- for handle in handles.rev() {
- handle.join().unwrap();
- }
- }
Add Comment
Please, Sign In to add comment