File tree 5 files changed +63
-10
lines changed
5 files changed +63
-10
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,10 @@ edition = "2021"
9
9
async-lock = " 2.5.0"
10
10
async-oneshot = " 0.5.0"
11
11
atomic_float = " 0.1.0"
12
+ atomicbox = " 0.4.0"
13
+ atomig = " 0.4.0"
12
14
awaitgroup = " 0.6.0"
15
+ barrage = " 0.2.3"
13
16
catty = " 0.1.5"
14
17
concurrent-queue = " 1.2.4"
15
18
dashmap = " 5.4.0"
Original file line number Diff line number Diff line change @@ -13,13 +13,15 @@ fn main() {
13
13
14
14
event_listener_example ( ) ;
15
15
triggered_example ( ) ;
16
-
16
+ barrage_example ( ) ;
17
+
17
18
hashmap_example ( ) ;
18
19
flurry_hashmap ( ) ;
19
20
flurry_hashset ( ) ;
20
21
evmap_example ( ) ;
21
22
22
23
concurrent_queue_example ( ) ;
24
+ triple_buffer_example ( ) ;
23
25
24
26
async_lock_mutex ( ) ;
25
27
async_lock_rwlock ( ) ;
@@ -30,6 +32,8 @@ fn main() {
30
32
portable_atomic_u128 ( ) ;
31
33
portable_atomic_f64 ( ) ;
32
34
atomic_float_example ( ) ;
35
+ atomig_example ( ) ;
36
+ atomicbox_examples ( ) ;
33
37
34
38
simple_mutex_example ( ) ;
35
39
Original file line number Diff line number Diff line change @@ -64,3 +64,13 @@ pub fn triggered_example() {
64
64
let _ = task. await ;
65
65
} )
66
66
}
67
+
68
+ pub fn barrage_example ( ) {
69
+ smol:: block_on ( async {
70
+ let ( tx, rx) = barrage:: unbounded ( ) ;
71
+ let rx2 = rx. clone ( ) ;
72
+ tx. send_async ( "Hello!" ) . await . unwrap ( ) ;
73
+ assert_eq ! ( rx. recv_async( ) . await , Ok ( "Hello!" ) ) ;
74
+ assert_eq ! ( rx2. recv_async( ) . await , Ok ( "Hello!" ) ) ;
75
+ } ) ;
76
+ }
Original file line number Diff line number Diff line change
1
+ use atomicbox:: AtomicBox ;
2
+ use atomig:: Atomic ;
1
3
use portable_atomic:: * ;
4
+ use std:: sync:: atomic:: Ordering ;
2
5
use std:: sync:: atomic:: Ordering :: Relaxed ;
3
6
4
7
pub fn portable_atomic_i128 ( ) {
@@ -52,3 +55,20 @@ pub fn atomic_float_example() {
52
55
53
56
assert_eq ! ( some_var. load( Relaxed ) , -885.0 ) ;
54
57
}
58
+
59
+ pub fn atomig_example ( ) {
60
+ let some_var = Atomic :: new ( 0 ) ;
61
+ some_var. store ( 800 , Relaxed ) ;
62
+
63
+ some_var. fetch_add ( 30 , Relaxed ) ;
64
+ some_var. fetch_sub ( -55 , Relaxed ) ;
65
+
66
+ assert_eq ! ( some_var. load( Relaxed ) , 885 ) ;
67
+ }
68
+
69
+ pub fn atomicbox_examples ( ) {
70
+ let atom = AtomicBox :: new ( Box :: new ( "one" ) ) ;
71
+ let mut boxed = Box :: new ( "two" ) ;
72
+ atom. swap_mut ( & mut boxed, Ordering :: AcqRel ) ;
73
+ assert_eq ! ( * boxed, "one" ) ;
74
+ }
Original file line number Diff line number Diff line change 1
1
use concurrent_queue:: ConcurrentQueue ;
2
- use std:: thread;
3
2
use std:: sync:: Arc ;
3
+ use std:: thread;
4
+
5
+ use triple_buffer:: triple_buffer;
4
6
5
7
pub fn concurrent_queue_example ( ) {
6
8
let q = Arc :: new ( ConcurrentQueue :: unbounded ( ) ) ;
@@ -13,17 +15,31 @@ pub fn concurrent_queue_example() {
13
15
} ) ;
14
16
15
17
let q2 = q. clone ( ) ;
16
- let rhandle = thread:: spawn ( move || {
17
- loop {
18
- if let Ok ( v) = q2. pop ( ) {
19
- println ! ( "get value {}" , v) ;
20
- } else {
21
- println ! ( "queue closed" ) ;
22
- break ;
23
- }
18
+ let rhandle = thread:: spawn ( move || loop {
19
+ if let Ok ( v) = q2. pop ( ) {
20
+ println ! ( "get value {}" , v) ;
21
+ } else {
22
+ println ! ( "queue closed" ) ;
23
+ break ;
24
24
}
25
25
} ) ;
26
26
27
27
whandle. join ( ) . unwrap ( ) ;
28
28
rhandle. join ( ) . unwrap ( ) ;
29
29
}
30
+
31
+ pub fn triple_buffer_example ( ) {
32
+ let ( mut buf_input, mut buf_output) = triple_buffer ( & 0 ) ;
33
+
34
+ // The producer thread can move a value into the buffer at any time
35
+ let producer = std:: thread:: spawn ( move || buf_input. write ( 42 ) ) ;
36
+
37
+ // The consumer thread can read the latest value at any time
38
+ let consumer = std:: thread:: spawn ( move || {
39
+ let latest = buf_output. read ( ) ;
40
+ assert ! ( * latest == 42 || * latest == 0 ) ;
41
+ } ) ;
42
+
43
+ producer. join ( ) . unwrap ( ) ;
44
+ consumer. join ( ) . unwrap ( ) ;
45
+ }
You can’t perform that action at this time.
0 commit comments