libASPL
|
Doubly-buffered value with non-blocking read and blocking write. More...
#include <aspl/DoubleBuffer.hpp>
Classes | |
class | ReadLock |
Read lock. More... | |
Public Member Functions | |
DoubleBuffer (const T &value) | |
Initialize buffer with given value. | |
DoubleBuffer (T &&value=T()) | |
Initialize buffer with given value. | |
DoubleBuffer (const DoubleBuffer &)=delete | |
DoubleBuffer & | operator= (const DoubleBuffer &)=delete |
ReadLock | GetReadLock () const |
Get locked read-only reference to the value. Non-blocking and lock-free, see ReadLock. | |
T | Get () const |
Get copy of the value. Non-blocking and lock-free, see ReadLock. | |
template<typename TT > | |
void | Set (TT &&value) |
Set value. | |
Friends | |
class | ReadLock |
Doubly-buffered value with non-blocking read and blocking write.
Logically, this is a single-value container, which has getter and setter with the following characteristics:
Physically container is implemented as two copies of the value:
The algorithm is optimized for the following use case:
The value stored in the double buffer is immutable after it's set. To change the value, you need to call the getter, make a copy, modify it, and pass it to the setter.
The value should have public default and copy constructors. If it also has move constructor, it can be used in setter.
If the value has non-trivial destructor, before setter returns, it waits until the previously used value is not accessed by readers anymore and invokes destructor for the old value.
Typical reader looks like the following:
Or, if the reader is okay to call the copy constructor:
Typical writer looks like this:
Definition at line 95 of file DoubleBuffer.hpp.
|
inlineexplicit |
Initialize buffer with given value.
Definition at line 197 of file DoubleBuffer.hpp.
|
inlineexplicit |
Initialize buffer with given value.
Definition at line 203 of file DoubleBuffer.hpp.
|
inline |
Get copy of the value. Non-blocking and lock-free, see ReadLock.
Definition at line 222 of file DoubleBuffer.hpp.
|
inline |
Get locked read-only reference to the value. Non-blocking and lock-free, see ReadLock.
Definition at line 215 of file DoubleBuffer.hpp.
|
inline |
Set value.
Blocks until all getters, invoked before this call, are finished. Concurrent setter calls are serialized.
It is guaranteed that if a getter is called after a setter returns, the getter will observe the updated value.
The value is either copied or moved into the internal buffer, depending on whether an lvalue or rvalue is passed.
Definition at line 240 of file DoubleBuffer.hpp.
|
friend |
Definition at line 303 of file DoubleBuffer.hpp.