We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 210776b commit 1f1dea3Copy full SHA for 1f1dea3
collections/src/lib.rs
@@ -0,0 +1,22 @@
1
+use std::{sync::{Arc, Mutex}, collections::HashMap};
2
+
3
4
+pub fn common_thread_safe_collections() {
5
+ let map: HashMap<i32, i32> = HashMap::new();
6
+ let m = Arc::new(Mutex::new(map));
7
8
+ let mut handles = vec![];
9
+ for i in 0..10 {
10
+ let m = Arc::clone(&m);
11
+ handles.push(std::thread::spawn(move || {
12
+ let mut map = m.lock().unwrap();
13
+ map.insert(i, i);
14
+ }));
15
+ }
16
17
+ for handle in handles {
18
+ handle.join().unwrap();
19
20
21
+ println!("HashMap: {:?}", *m.lock().unwrap());
22
+}
collections/src/main.rs
@@ -1,5 +1,5 @@
-use collections;
+use collections::*;
fn main() {
- println!("Hello, world!");
+ common_thread_safe_collections();
}
0 commit comments