Skip to content

Commit 49879c5

Browse files
committed
add chapter 06
1 parent 12ed38b commit 49879c5

File tree

6 files changed

+96
-15
lines changed

6 files changed

+96
-15
lines changed

.DS_Store

0 Bytes
Binary file not shown.

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,44 @@ Introduces [async feature](asyncwait/src/main.rs).
2525

2626
### chapter 4: synchronization primitives
2727

28-
Introduces synchronization primitives contains [containers](container_primitive/src/main.rs) and [primitives](sync_primitive/src/main.rs) in std lib.
28+
Introduces synchronization primitives contains [containers](container_primitive/src/main.rs)
2929

30-
### chapter 5: concurrency collections
30+
### chapter 5: basic concurrency primitives
31+
Introduction of basic concurrency [primitives](sync_primitive/src/main.rs) in std lib.
32+
33+
### chapter 6: concurrency collections
3134

3235
Introduces [concurrency collections](collections/src/main.rs) in std lib.
3336

34-
### chapter 6: process
37+
### chapter 7: process
3538

3639
Introduces starting and executing a new [process](process/src/main.rs) in the easy way.
3740

38-
### chapter 7: channel
41+
### chapter 8: channel
3942

4043
Introduces each [channels](channel/src/main.rs) such as mpsc, mpmc and broadcasters.
4144

42-
### chapter 8: timer/ticker
45+
### chapter 9: timer/ticker
4346

4447
Introduces [timer and ticker](timer_examples/src/main.rs).
4548

46-
### chapter 9: parking_lot
49+
### chapter 10: parking_lot
4750

4851
Introduces [parking_lot](parking_lot_examples/src/main.rs).
4952

50-
### chapter 10: crossbeam
53+
### chapter 11: crossbeam
5154

5255
Introduces [crossbeam](crossbeam_examples/src/main.rs).
5356

54-
### chapter 11: rayon
57+
### chapter 12: rayon
5558

5659
Introduces [rayon](rayon_examples/src/main.rs).
5760

58-
### chapter 12: tokio
61+
### chapter 13: tokio
5962

6063
Introduces [tokio](tokio_examples/src/main.rs).
6164

62-
63-
### chapter n: special
65+
### chapter 14: special
6466

6567
some special synchronization primitives and concurrency libs only for special single purpose.
6668

book_cn/rust_concurrency_cookbook.pdf

27.6 KB
Binary file not shown.

collections/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
cuckoofilter = "0.5.0"
10+
dashmap = "5.5.3"
11+
evmap = "10.0.2"

collections/src/lib.rs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
use std::{sync::{Arc, Mutex}, collections::HashMap};
1+
use cuckoofilter::CuckooFilter;
2+
use dashmap::DashMap;
23
use std::collections::LinkedList;
4+
use std::{
5+
collections::HashMap,
6+
sync::{Arc, Mutex},
7+
};
38

49
pub fn common_thread_safe_collections() {
510
let map: HashMap<i32, i32> = HashMap::new();
@@ -21,7 +26,6 @@ pub fn common_thread_safe_collections() {
2126
println!("HashMap: {:?}", *m.lock().unwrap());
2227
}
2328

24-
2529
pub fn common_thread_safe_vec() {
2630
let vec1 = vec![];
2731
let vec2 = Arc::new(Mutex::new(vec1));
@@ -39,7 +43,7 @@ pub fn common_thread_safe_vec() {
3943
handle.join().unwrap();
4044
}
4145

42-
println!("vec: {:?}", vec2.lock().unwrap());
46+
println!("vec: {:?}", vec2.lock().unwrap());
4347
}
4448

4549
pub fn common_thread_safe_linkedlist() {
@@ -59,5 +63,74 @@ pub fn common_thread_safe_linkedlist() {
5963
handle.join().unwrap();
6064
}
6165

62-
println!("LinkedList: {:?}", list2.lock().unwrap());
66+
println!("LinkedList: {:?}", list2.lock().unwrap());
67+
}
68+
69+
pub fn dashmap_example() {
70+
let map = Arc::new(DashMap::new());
71+
let mut handles = vec![];
72+
73+
for i in 0..10 {
74+
let map = Arc::clone(&map);
75+
handles.push(std::thread::spawn(move || {
76+
map.insert(i, i);
77+
}));
78+
}
79+
80+
for handle in handles {
81+
handle.join().unwrap();
82+
}
83+
84+
println!("DashMap: {:?}", map);
85+
}
86+
87+
pub fn cuckoofilter_example() {
88+
let value: &str = "hello world";
89+
90+
// Create cuckoo filter with default max capacity of 1000000 items
91+
let mut cf = CuckooFilter::new();
92+
93+
// Add data to the filter
94+
cf.add(value).unwrap();
95+
96+
// Lookup if data is in the filter
97+
let success = cf.contains(value);
98+
assert!(success);
99+
100+
// Test and add to the filter (if data does not exists then add)
101+
let success = cf.test_and_add(value).unwrap();
102+
assert!(!success);
103+
104+
// Remove data from the filter.
105+
let success = cf.delete(value);
106+
assert!(success);
107+
}
108+
109+
pub fn evmap_example() {
110+
let (book_reviews_r, book_reviews_w) = evmap::new();
111+
112+
// start some writers.
113+
// since evmap does not support concurrent writes, we need
114+
// to protect the write handle by a mutex.
115+
let w = Arc::new(Mutex::new(book_reviews_w));
116+
let writers: Vec<_> = (0..4)
117+
.map(|i| {
118+
let w = w.clone();
119+
std::thread::spawn(move || {
120+
let mut w = w.lock().unwrap();
121+
w.insert(i, true);
122+
w.refresh();
123+
})
124+
})
125+
.collect();
126+
127+
// eventually we should see all the writes
128+
while book_reviews_r.len() < 4 {
129+
std::thread::yield_now();
130+
}
131+
132+
// all the threads should eventually finish writing
133+
for w in writers.into_iter() {
134+
assert!(w.join().is_ok());
135+
}
63136
}

collections/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ fn main() {
55
common_thread_safe_vec();
66
common_thread_safe_linkedlist();
77

8+
dashmap_example();
9+
cuckoofilter_example();
10+
evmap_example();
811
}

0 commit comments

Comments
 (0)