| Home | Libraries | People | FAQ | More

## Struct template spsc_value

boost::lockfree::spsc_value

## Synopsis

    // 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 &&);
    };

## Description

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 functions

1.     explicit spsc_value();

   Construct a spsc_value

   If configured with boost::lockfree::allow_multiple_reads<true> it is initialized to a default-constructed value

2.     explicit spsc_value(T value);

   Construct a spsc_value, initialized to a value

3.     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

4.     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

5.     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 `ret`.
   Returns: | `true`, if the read operation is successful, false if the spsc_value is configured with boost::lockfree::allow_multiple_reads<false> and no value is available for reading

6.     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: | `std::optional` with value if successful, `std::nullopt` if spsc_value is configured with boost::lockfree::allow_multiple_reads<false> and no value is available for reading

7.     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: | `true`, if element was consumed