...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::lockfree::spsc_value
// In header: <boost/lockfree/spsc_value.hpp> template<typename T, typename... Options> struct spsc_value { // public member functions explicit spsc_value(); explicit spsc_value(T); void write(T &&); void write(const T &); bool read(T &); std::optional< T > read(uses_optional_t); template<typename Functor> bool consume(Functor &&); };
The spcs_value provides a single-writer/single-reader value, implemented by a triple buffer
Policies:
boost::lockfree::allow_multiple_reads, defaults to boost::lockfree::allow_multiple_reads<false>
If multiple reads are allowed, a value written to the spsc_value can be read multiple times, but not moved out of the instance. If multiple reads are not allowed, the class works as single-element queue that overwrites on write
spsc_value
public member functionsexplicit spsc_value();
Construct a spsc_value
If configured with boost::lockfree::allow_multiple_reads<true> it is initialized to a default-constructed value
explicit spsc_value(T value);
Construct a spsc_value, initialized to a value
void write(T && value);
Writes value
to the spsc_value
![]() |
Note |
---|---|
Thread-safe and wait-free |
Requires: |
only one thread is allowed to write data to the spsc_value |
Postconditions: |
object will be written to the spsc_value |
void write(const T & value);
Writes value
to the spsc_value
![]() |
Note |
---|---|
Thread-safe and wait-free |
Requires: |
only one thread is allowed to write data to the spsc_value |
Postconditions: |
object will be written to the spsc_value |
bool read(T & ret);
Reads content of the spsc_value
![]() |
Note |
---|---|
Thread-safe and wait-free |
Requires: |
only one thread is allowed to write data to the spsc_value |
Postconditions: |
if read operation is successful, object will be copied to |
Returns: |
|
std::optional< T > read(uses_optional_t);
Reads content of the spsc_value, returning an optional
![]() |
Note |
---|---|
Thread-safe and wait-free |
Requires: |
only one thread is allowed to write data to the spsc_value |
Returns: |
|
template<typename Functor> bool consume(Functor && f);
consumes value via a functor
reads element from the spsc_value and applies the functor on this object
![]() |
Note |
---|---|
Thread-safe and non-blocking, if functor is thread-safe and non-blocking |
Returns: |
|