Skip to content

Commit a47c716

Browse files
authored
Merge pull request #40 from HadrienG2/update-tutorial
Update tutorial from the README
2 parents 27e1e70 + 6842419 commit a47c716

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

README.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ useful for the following class of thread synchronization problems:
1717
- The producer wants to update a shared memory value periodically
1818
- The consumer wants to access the latest update from the producer at any time
1919

20-
The simplest way to use it is as follows:
20+
For many use cases, you can use the ergonomic write/read interface, where
21+
the producer moves values into the buffer and the consumer accesses the
22+
latest buffer by shared reference:
2123

2224
```rust
2325
// Create a triple buffer
@@ -32,6 +34,9 @@ let consumer = std::thread::spawn(move || {
3234
let latest = buf_output.read();
3335
assert!(*latest == 42 || *latest == 0);
3436
});
37+
38+
# producer.join().unwrap();
39+
# consumer.join().unwrap();
3540
```
3641

3742
In situations where moving the original value away and being unable to
@@ -45,10 +50,12 @@ and to precisely control when updates are propagated:
4550
use triple_buffer::triple_buffer;
4651
let (mut buf_input, mut buf_output) = triple_buffer(&String::with_capacity(42));
4752

53+
// --- PRODUCER SIDE ---
54+
4855
// Mutate the input buffer in place
4956
{
5057
// Acquire a reference to the input buffer
51-
let input = buf_input.input_buffer();
58+
let input = buf_input.input_buffer_mut();
5259

5360
// In general, you don't know what's inside of the buffer, so you should
5461
// always reset the value before use (this is a type-specific process).
@@ -61,14 +68,52 @@ let (mut buf_input, mut buf_output) = triple_buffer(&String::with_capacity(42));
6168
// Publish the above input buffer update
6269
buf_input.publish();
6370

71+
// --- CONSUMER SIDE ---
72+
6473
// Manually fetch the buffer update from the consumer interface
6574
buf_output.update();
6675

67-
// Acquire a mutable reference to the output buffer
68-
let output = buf_output.output_buffer();
76+
// Acquire read-only reference to the output buffer
77+
let output = buf_output.peek_output_buffer();
78+
assert_eq!(*output, "Hello, ");
79+
80+
// Or acquire mutable reference if necessary
81+
let output_mut = buf_output.output_buffer_mut();
6982

7083
// Post-process the output value before use
71-
output.push_str("world!");
84+
output_mut.push_str("world!");
85+
```
86+
87+
Finally, as a middle ground before the maximal ergonomics of the
88+
[`write()`](Input::write) API and the maximal control of the
89+
[`input_buffer_mut()`](Input::input_buffer_mut)/[`publish()`](Input::publish)
90+
API, you can also use the
91+
[`input_buffer_publisher()`](Input::input_buffer_publisher) RAII API on the
92+
producer side, which ensures that `publish()` is automatically called when
93+
the resulting input buffer handle goes out of scope:
94+
95+
```rust
96+
// Create and split a triple buffer
97+
use triple_buffer::triple_buffer;
98+
let (mut buf_input, _) = triple_buffer(&String::with_capacity(42));
99+
100+
// Mutate the input buffer in place and publish it
101+
{
102+
// Acquire a reference to the input buffer
103+
let mut input = buf_input.input_buffer_publisher();
104+
105+
// In general, you don't know what's inside of the buffer, so you should
106+
// always reset the value before use (this is a type-specific process).
107+
input.clear();
108+
109+
// Perform an in-place update
110+
input.push_str("Hello world!");
111+
112+
// Input buffer is automatically published at the end of the scope of
113+
// the "input" RAII guard
114+
}
115+
116+
// From this point on, the consumer can see the updated version
72117
```
73118

74119

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! Triple buffering in Rust
2-
//!
31
//! In this crate, we propose a Rust implementation of triple buffering. This is
42
//! a non-blocking thread synchronization mechanism that can be used when a
53
//! single producer thread is frequently updating a shared data block, and a

0 commit comments

Comments
 (0)