File threadsafe_object.hpp¶
This file declares templated container for data buffering.
- Author
Manuel Wuthrich (manuel.wuthrich@gmail.com)
- Author
Maximilien Naveau (maximilien.naveau@gmail.com)
- Version
0.1
- Date
2018-11-27
- Copyright
Copyright (c) 2018
-
namespace real_time_tools
-
template<typename Type, size_t SIZE>
class SingletypeThreadsafeObject - #include <threadsafe_object.hpp>
The SingletypeThreadsafeObject is a thread safe object.
- Template Parameters:
Type – is the data type to store in the buffer.
SIZE – is the size of the buffer. It is better to know it at compile time to be 100% real time safe.
Public Functions
-
SingletypeThreadsafeObject()
Construct a new SingletypeThreadsafeObject object.
-
SingletypeThreadsafeObject(const std::vector<std::string> &names)
Construct a new SingletypeThreadsafeObject object.
- Parameters:
names –
-
void wait_for_update(const size_t &index) const
Wait until the data at the given index is modified.
- Parameters:
index –
-
inline void wait_for_update(const std::string &name) const
Wait until the data at the given name is modified.
- Parameters:
name –
-
size_t wait_for_update() const
Wait unitl any data has been changed and return its index.
- Returns:
size_t
-
inline size_t size()
Getters.
get size.
- Returns:
size_t
-
inline Type get(const size_t &index = 0) const
Get the data by its index in the buffer.
- Parameters:
index –
- Returns:
Type
-
inline Type get(const std::string &name) const
Get the data by its name in the buffer.
- Parameters:
name –
- Returns:
Type
-
template<int INDEX = 0>
inline Type get() const Get the data by its index in the buffer.
Index is solved during compile time
- Template Parameters:
INDEX=0 –
- Returns:
Type
-
void set(const Type &datum, const size_t &index = 0)
Setters.
Set one element at a designated index.
- Parameters:
datum –
index –
-
template<int INDEX = 0>
inline void set(Type datum) Set one element at a designated index.
Warning the index is resolved at compile time. This is used for backward comaptibility.
- Todo:
”This is used for backward comaptibility.”, Manuel Which bakward?
- Template Parameters:
INDEX=0 –
- Parameters:
datum –
-
inline void set(const Type &datum, const std::string &name)
Set one element using at a designated name.
Internally this name is map to an index.
- Parameters:
datum –
name –
Private Members
-
std::shared_ptr<std::array<size_t, SIZE>> modification_counts_
This is counting the data modification occurences for each individual buffers.
-
std::shared_ptr<size_t> total_modification_count_
This is counting the all data modification occurences for all buffer.
/todo Can’t we just some the modification_counts_ array whenever needed?
-
std::map<std::string, size_t> name_to_index_
This is the map that allow to deal with data by their names.
-
mutable std::shared_ptr<std::condition_variable> condition_
This condition variable is used to wait untils any data has been changed.
-
mutable std::shared_ptr<std::mutex> condition_mutex_
This is the mutex of the condition varaible.
-
mutable std::shared_ptr<std::array<std::mutex, SIZE>> data_mutexes_
These are the individual mutexes of each data upon setting and getting.
-
template<typename Type>
class ThreadsafeHistoryInterface - #include <threadsafe_object.hpp>
This is a template abstract interface class that define a data history.
This re-writting of the vector style class is thread safe. So it allows the user to use the object without having to deal with mutexes nor condition variables. This class is not used so far.
- Template Parameters:
Type – is the type of the data to store.
Private Functions
-
inline virtual Type get_next(size_t id) const
Get the element after the one with the given id.
if there is no newer element, then wait until one arrives.
- Parameters:
id – is the index of the element in the buffer.
- Returns:
Type the next element.
-
virtual size_t get_next_id(size_t id) const = 0
-
inline virtual Type get_newest() const
Get the newest value, this function waits if it is empty.
- Returns:
Type the newest element.
-
virtual size_t get_newest_id() const = 0
get_newest_id
- Returns:
size_t
-
virtual Type get(size_t id) const = 0
Get the value whith a specific id.
- Parameters:
id –
- Returns:
Type
-
virtual void add() = 0
I guess this is to add a value but with no argument?
- Todo:
Manuel, could you delete this class or provide an implementation?
-
template<typename ...Types>
class ThreadsafeObject - #include <threadsafe_object.hpp>
This object can have several types depending on what ones want to store.
- Template Parameters:
Types –
Public Types
Public Functions
-
ThreadsafeObject()
Construct a new ThreadsafeObject object.
-
void wait_for_update(unsigned index) const
Wait until the data with the deignated index is changed.
- Parameters:
index –
-
template<unsigned INDEX = 0>
inline void wait_for_update() const Wait until the data with the designated index is changed.
- Template Parameters:
INDEX=0 –
-
size_t wait_for_update() const
Wait until any data has been changed.
- Returns:
size_t
Public Static Attributes
-
static const std::size_t SIZE = sizeof...(Types)
Define the size of the different types.
Private Members
-
std::shared_ptr<std::tuple<Types...>> data_
the actual data buffers.
-
mutable std::shared_ptr<std::condition_variable> condition_
a condition variable that allow to wait until one data has been changed in the buffer.
-
mutable std::shared_ptr<std::mutex> condition_mutex_
The mutex of the condition variable.
-
std::shared_ptr<std::array<size_t, SIZE>> modification_counts_
This is counting the data modification occurences for each individual buffers.
-
std::shared_ptr<size_t> total_modification_count_
This is counting the all data modification occurences for all buffer.
/todo Can’t we just some the modification_counts_ array whenever needed?
-
std::shared_ptr<std::array<std::mutex, SIZE>> data_mutexes_
These are the individual mutexes of each data upon setting and getting.
-
template<typename Type, size_t SIZE>