File tree 6 files changed +71
-2
lines changed
6 files changed +71
-2
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ edition = "2021"
6
6
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
7
8
8
[dependencies ]
9
+ arc-swap = " 1.6.0"
9
10
async-lock = " 2.5.0"
10
11
async-oneshot = " 0.5.0"
11
12
async-weighted-semaphore = " 0.2.1"
@@ -17,6 +18,7 @@ awaitgroup = "0.6.0"
17
18
barrage = " 0.2.3"
18
19
catty = " 0.1.5"
19
20
concurrent-queue = " 1.2.4"
21
+ crossbeam-utils = " 0.8.14"
20
22
dashmap = " 5.4.0"
21
23
event-listener = " 2.5.3"
22
24
evmap = " 10.0.2"
@@ -31,6 +33,7 @@ simple-mutex = "1.1.5"
31
33
singleflight-async = " 0.1.1"
32
34
slab = " 0.4.7"
33
35
smol = " 1.2.5"
36
+ sync_cow = " 0.1.1"
34
37
tokio = { version = " 1.21.2" , features = [" full" ] }
35
38
triggered = " 0.1.2"
36
39
triple_buffer = " 6.2.0"
Original file line number Diff line number Diff line change
1
+
2
+ use arc_swap:: ArcSwap ;
3
+ use std:: sync:: Arc ;
4
+ use crossbeam_utils:: thread;
5
+
6
+ pub fn arc_swap_example ( ) {
7
+ let value = ArcSwap :: from ( Arc :: new ( 5 ) ) ;
8
+ thread:: scope ( |scope| {
9
+ scope. spawn ( |_| {
10
+ let new_value = Arc :: new ( 4 ) ;
11
+ value. store ( new_value) ;
12
+ } ) ;
13
+ for _ in 0 ..10 {
14
+ scope. spawn ( |_| {
15
+ loop {
16
+ let v = value. load ( ) ;
17
+ println ! ( "value is {}" , v) ;
18
+ return ;
19
+ }
20
+ } ) ;
21
+ }
22
+ } ) . unwrap ( )
23
+
24
+ }
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ mod queue;
7
7
mod scc_examples;
8
8
mod sema_examples;
9
9
mod singleflight_example;
10
+ mod synccow;
11
+ mod arcswap;
10
12
11
13
pub use oslock:: * ;
12
14
pub use oneshots:: * ;
@@ -16,4 +18,6 @@ pub use notify::*;
16
18
pub use queue:: * ;
17
19
pub use scc_examples:: * ;
18
20
pub use sema_examples:: * ;
19
- pub use singleflight_example:: * ;
21
+ pub use singleflight_example:: * ;
22
+ pub use synccow:: * ;
23
+ pub use arcswap:: * ;
Original file line number Diff line number Diff line change @@ -58,6 +58,9 @@ fn main() {
58
58
singleflight_example ( ) ;
59
59
async_singleflight_example ( ) ;
60
60
61
+ sync_cow_example ( ) . unwrap ( ) ;
62
+ arc_swap_example ( ) ;
63
+
61
64
}
62
65
63
66
Original file line number Diff line number Diff line change 1
1
use futures:: future:: join_all;
2
2
use singleflight_async:: SingleFlight ;
3
3
use std:: sync:: Arc ;
4
- use std:: time:: Duration ;
5
4
6
5
use async_singleflight:: Group ;
7
6
Original file line number Diff line number Diff line change
1
+ use sync_cow:: SyncCow ;
2
+ use std:: sync:: Arc ;
3
+ use std:: any:: Any ;
4
+
5
+
6
+ pub fn sync_cow_example ( ) -> Result < ( ) , Box < dyn Any + Send > > {
7
+ let cow = Arc :: new ( SyncCow :: new ( 5 ) ) ;
8
+
9
+ // Arc is only needed to pass the ref to the threads
10
+ let cow_write_arc = cow. clone ( ) ;
11
+ let cow_read_arc = cow. clone ( ) ;
12
+ let cow_result_arc = cow. clone ( ) ;
13
+
14
+ let writer = std:: thread:: spawn ( move || {
15
+ let cow = & * cow_write_arc; // unpack immediately to avoid Arc deref
16
+ let mut val = 0 ;
17
+ cow. edit ( |x| {
18
+ val = * x;
19
+ * x = 4 ;
20
+ } ) ;
21
+ println ! ( "Cow was {} when writing" , val) ;
22
+ } ) ;
23
+
24
+ let reader = std:: thread:: spawn ( move || {
25
+ let cow = & * cow_read_arc; // unpack immediately to avoid Arc deref
26
+ println ! ( "Cow was {} when reading" , cow. read( ) ) ;
27
+ } ) ;
28
+
29
+ writer. join ( ) ?;
30
+ reader. join ( ) ?;
31
+
32
+ let cow = & * cow_result_arc; // unpack immediately to avoid Arc deref
33
+ println ! ( "Cow was {} when result" , cow. read( ) ) ;
34
+
35
+ Ok ( ( ) )
36
+ }
You can’t perform that action at this time.
0 commit comments