iceoryx_doc  1.0.1
Classes | Public Member Functions | List of all members
iox::concurrent::TACO< T, Context, MaxNumberOfContext > Class Template Reference

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 Member Functions

 TACO (TACOMode mode)
 
 TACO (const TACO &)=delete
 
 TACO (TACO &&)=delete
 
TACOoperator= (const TACO &)=delete
 
TACOoperator= (TACO &&)=delete
 
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< T, Context, MaxNumberOfContext >

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
TDataType to be stored
ContextEnum 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;
}

Constructor & Destructor Documentation

◆ TACO()

template<typename T , typename Context , uint32_t MaxNumberOfContext = 500>
iox::concurrent::TACO< T, Context, MaxNumberOfContext >::TACO ( TACOMode  mode)
inline

Create a TACO instance with the specified mode

Parameters
[in]modethe TACO operates

Member Function Documentation

◆ exchange()

template<typename T , typename Context , uint32_t MaxNumberOfContext = 500>
cxx::optional<T> iox::concurrent::TACO< T, Context, MaxNumberOfContext >::exchange ( const T &  data,
Context  context 
)
inline

Takes the data from the TACO and supplies new data

Parameters
[in]datato supply for consumption, it's copied into a local cache in the TACO
[in]contextof the thread which performs the exchange
Returns
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

◆ store()

template<typename T , typename Context , uint32_t MaxNumberOfContext = 500>
void iox::concurrent::TACO< T, Context, MaxNumberOfContext >::store ( const T &  data,
const Context  context 
)
inline

Supplies data for consumption

Parameters
[in]datato supply for consumption, it's copied into a local cache in the TACO
[in]contextof the thread which performs the exchange

◆ take()

template<typename T , typename Context , uint32_t MaxNumberOfContext = 500>
cxx::optional<T> iox::concurrent::TACO< T, Context, MaxNumberOfContext >::take ( const Context  context)
inline

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

Parameters
[in]contextof the thread which takes the data
Returns
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

The documentation for this class was generated from the following file: