Skip to content

iox::concurrent::TACO🔗

TACO is an acronym for Thread Aware exChange Ownership. Exchanging data between thread needs some synchonization mechanism. This can be done with a mutex or atomics. If the data structure is larger than 64 bit or if more than one value need to be accessed in a synchronized manner, a mutex would be the only option. The TACO is a wait-free alternative to the mutex. Data can be exchanged between threads. The TACO is like a SoFi with one element, but with the possibility to read/write from multiple threads. More...

#include <taco.hpp>

Public Functions🔗

Name
TACO(TACOMode mode)
TACO(const TACO & )
TACO(TACO && )
TACO & operator=(const TACO & )
TACO & operator=(TACO && )
cxx::optional< T > exchange(const T & data, Context context)
cxx::optional< T > take(const Context context)
void store(const T & data, const Context context)

Detailed Description🔗

template <typename T ,
typename Context ,
uint32_t MaxNumberOfContext =500>
class iox::concurrent::TACO;

TACO is an acronym for Thread Aware exChange Ownership. Exchanging data between thread needs some synchonization mechanism. This can be done with a mutex or atomics. If the data structure is larger than 64 bit or if more than one value need to be accessed in a synchronized manner, a mutex would be the only option. The TACO is a wait-free alternative to the mutex. Data can be exchanged between threads. The TACO is like a SoFi with one element, but with the possibility to read/write from multiple threads.

Parameters:

  • T DataType to be stored
  • Context Enum class with all the thread context that access the TACO. The enum must start with 0, must have ascending values and the last vaule must be called END_OF_LIST.
#include "iceoryx_utils/internal/concurrent/taco.hpp"

#include <cstdint>
#include <iostream>
#include <thread>

constexpr std::uint64_t TotalCount{1000000};
struct SyncedData
{
    std::uint64_t decrementCounter{TotalCount};
    std::uint64_t incrementCounter{0};
};

enum class ThreadContext : uint32_t
{
    Hardy,
    Laurel,
    END_OF_LIST
};

int main()
{
    concurrent::TACO<SyncedData, ThreadContext> taco(concurrent::TACOMode::DenyDataFromSameContext);
    constexpr auto producerContext {ThreadContext::Hardy};
    constexpr auto consumerContext {ThreadContext::Laurel};

    auto producer = std::thread([&] {
        SyncedData data;
        while (data.decrementCounter != 0)
        {
            data.decrementCounter--;
            data.incrementCounter++;
            taco.store(data, producerContext);
        }
    });
    auto consumer = std::thread([&] {
        SyncedData data;
        do
        {
            auto retVal = taco.take(consumerContext);
            if (retVal.has_value())
            {
                data = *retVal;
                if(data.decrementCounter + data.incrementCounter != TotalCount)
                {
                    std::cout << "Error! Counter not synchronized!" << std::endl;
                }
            }

        } while (data.decrementCounter != 0);
    });

    producer.join();
    consumer.join();

    std::cout << "Finished!" << std::endl;

    return 0;
}

Public Functions Documentation🔗

function TACO🔗

inline TACO(
    TACOMode mode
)

Parameters:

  • mode the TACO operates

Create a TACO instance with the specified mode

function TACO🔗

TACO(
    const TACO & 
)

function TACO🔗

TACO(
    TACO && 
)

function operator=🔗

TACO & operator=(
    const TACO & 
)

function operator=🔗

TACO & operator=(
    TACO && 
)

function exchange🔗

inline cxx::optional< T > exchange(
    const T & data,
    Context context
)

Parameters:

  • data to supply for consumption, it's copied into a local cache in the TACO
  • context of the thread which performs the exchange

Return: the data a previous operation supplied for consumption or nullopt_t if there was either no data or the data was supplied from the same context and the mode disallows data from the same context

Takes the data from the TACO and supplies new data

function take🔗

inline cxx::optional< T > take(
    const Context context
)

Parameters:

  • context of the thread which takes the data

Return: the data a previous operation supplied for consumption or nullopt_t if there was either no data or the data was supplied from the same context and the mode disallows data from the same context

Takes the data which is ready for consumption. The data isn't available for other access anymore.

function store🔗

inline void store(
    const T & data,
    const Context context
)

Parameters:

  • data to supply for consumption, it's copied into a local cache in the TACO
  • context of the thread which performs the exchange

Supplies data for consumption


Updated on 17 June 2021 at 11:15:26 CEST