t1.join().unwrap();
+ // message passing
let (tx1, rx) = sync::mpsc::channel();
let tx2 = tx1.clone();
sender1.join().unwrap();
sender2.join().unwrap();
+
+ // shared state
+ let counter = sync::Arc::new(sync::Mutex::new(0));
+ let mut threads = vec![];
+ for _ in 0..10 {
+ let tc = sync::Arc::clone(&counter);
+ threads.push(thread::spawn(move || {
+ *tc.lock().unwrap() += 1;
+ }));
+ }
+
+ for t in threads {
+ t.join().unwrap();
+ }
+
+ println!("counter: {}", *counter.lock().unwrap());
}
fn main() {