13 #include <condition_variable> 21 template<
class MY_TYPE=
int32_t>
class Fifo {
24 std::condition_variable m_condition;
25 std::vector<MY_TYPE> m_data;
45 bool wait(MY_TYPE &_data) {
46 std::unique_lock<std::mutex> lock(m_mutex);
48 while(m_data.size() == 0) {
49 m_condition.wait(lock);
52 if (m_data.size() > 0) {
54 std::swap(_data, m_data[0]);
56 m_data.erase(m_data.begin());
68 bool wait(MY_TYPE &_data, uint32_t _timeOutInUs) {
69 std::unique_lock<std::mutex> lock(m_mutex);
71 while(m_data.size() == 0) {
72 if (m_condition.wait_for(lock, std::chrono::microseconds(_timeOutInUs)) == std::cv_status::timeout) {
77 if (m_data.size() > 0) {
79 std::swap(_data, m_data[0]);
91 std::unique_lock<std::mutex> lock(m_mutex);
92 int32_t nbElement = m_data.size();
99 void post(MY_TYPE &_data) {
100 std::unique_lock<std::mutex> lock(m_mutex);
101 m_data.push_back(_data);
102 m_condition.notify_all();
108 void post(
const MY_TYPE &_data) {
109 std::unique_lock<std::mutex> lock(m_mutex);
110 m_data.push_back(_data);
111 m_condition.notify_all();
117 std::unique_lock<std::mutex> lock(m_mutex);
120 m_condition.wait_for(lock, std::chrono::microseconds(0));
void clean()
Remove all the message in the fifo.
Definition: Fifo.hpp:116
void post(MY_TYPE &_data)
Send a message at the other thread by setting a new message in the fifo.
Definition: Fifo.hpp:99
void post(const MY_TYPE &_data)
Send a message at the other thread by setting a new message in the fifo.
Definition: Fifo.hpp:108
int32_t count()
Get the number of message in the fifo.
Definition: Fifo.hpp:90
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
Fifo()
Create a fifo with no message.
Definition: Fifo.hpp:30
~Fifo()
Remove the fifo and all message inside.
Definition: Fifo.hpp:36
Fifo tamplate is a simple messaged fifo element to transfer data message from a thead to an other...
Definition: Fifo.hpp:21
bool wait(MY_TYPE &_data)
Wait a message from the other thread. (no timeout set)
Definition: Fifo.hpp:45
bool wait(MY_TYPE &_data, uint32_t _timeOutInUs)
Wait a message from the other thread, with a specified timeout.
Definition: Fifo.hpp:68