File tree Expand file tree Collapse file tree 6 files changed +71
-2
lines changed Expand file tree Collapse file tree 6 files changed +71
-2
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ edition = "2021"
66# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77
88[dependencies ]
9+ arc-swap = " 1.6.0"
910async-lock = " 2.5.0"
1011async-oneshot = " 0.5.0"
1112async-weighted-semaphore = " 0.2.1"
@@ -17,6 +18,7 @@ awaitgroup = "0.6.0"
1718barrage = " 0.2.3"
1819catty = " 0.1.5"
1920concurrent-queue = " 1.2.4"
21+ crossbeam-utils = " 0.8.14"
2022dashmap = " 5.4.0"
2123event-listener = " 2.5.3"
2224evmap = " 10.0.2"
@@ -31,6 +33,7 @@ simple-mutex = "1.1.5"
3133singleflight-async = " 0.1.1"
3234slab = " 0.4.7"
3335smol = " 1.2.5"
36+ sync_cow = " 0.1.1"
3437tokio = { version = " 1.21.2" , features = [" full" ] }
3538triggered = " 0.1.2"
3639triple_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;
77mod scc_examples;
88mod sema_examples;
99mod singleflight_example;
10+ mod synccow;
11+ mod arcswap;
1012
1113pub use oslock:: * ;
1214pub use oneshots:: * ;
@@ -16,4 +18,6 @@ pub use notify::*;
1618pub use queue:: * ;
1719pub use scc_examples:: * ;
1820pub 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() {
5858 singleflight_example ( ) ;
5959 async_singleflight_example ( ) ;
6060
61+ sync_cow_example ( ) . unwrap ( ) ;
62+ arc_swap_example ( ) ;
63+
6164}
6265
6366
Original file line number Diff line number Diff line change 11use futures:: future:: join_all;
22use singleflight_async:: SingleFlight ;
33use std:: sync:: Arc ;
4- use std:: time:: Duration ;
54
65use async_singleflight:: Group ;
76
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