Skip to content

Commit c50abbf

Browse files
committed
add more examples
1 parent 82c36a2 commit c50abbf

File tree

5 files changed

+63
-10
lines changed

5 files changed

+63
-10
lines changed

special/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ edition = "2021"
99
async-lock = "2.5.0"
1010
async-oneshot = "0.5.0"
1111
atomic_float = "0.1.0"
12+
atomicbox = "0.4.0"
13+
atomig = "0.4.0"
1214
awaitgroup = "0.6.0"
15+
barrage = "0.2.3"
1316
catty = "0.1.5"
1417
concurrent-queue = "1.2.4"
1518
dashmap = "5.4.0"

special/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ fn main() {
1313

1414
event_listener_example();
1515
triggered_example();
16-
16+
barrage_example();
17+
1718
hashmap_example();
1819
flurry_hashmap();
1920
flurry_hashset();
2021
evmap_example();
2122

2223
concurrent_queue_example();
24+
triple_buffer_example();
2325

2426
async_lock_mutex();
2527
async_lock_rwlock();
@@ -30,6 +32,8 @@ fn main() {
3032
portable_atomic_u128();
3133
portable_atomic_f64();
3234
atomic_float_example();
35+
atomig_example();
36+
atomicbox_examples();
3337

3438
simple_mutex_example();
3539

special/src/notify.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,13 @@ pub fn triggered_example() {
6464
let _ = task.await;
6565
})
6666
}
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+
}

special/src/primitive/atomic_examples.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use atomicbox::AtomicBox;
2+
use atomig::Atomic;
13
use portable_atomic::*;
4+
use std::sync::atomic::Ordering;
25
use std::sync::atomic::Ordering::Relaxed;
36

47
pub fn portable_atomic_i128() {
@@ -52,3 +55,20 @@ pub fn atomic_float_example() {
5255

5356
assert_eq!(some_var.load(Relaxed), -885.0);
5457
}
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+
}

special/src/queue.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use concurrent_queue::ConcurrentQueue;
2-
use std::thread;
32
use std::sync::Arc;
3+
use std::thread;
4+
5+
use triple_buffer::triple_buffer;
46

57
pub fn concurrent_queue_example() {
68
let q = Arc::new(ConcurrentQueue::unbounded());
@@ -13,17 +15,31 @@ pub fn concurrent_queue_example() {
1315
});
1416

1517
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;
2424
}
2525
});
2626

2727
whandle.join().unwrap();
2828
rhandle.join().unwrap();
2929
}
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+
}

0 commit comments

Comments
 (0)