forked from fedetft/miosix-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncQueue.h
56 lines (44 loc) · 939 Bytes
/
SyncQueue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* File: SyncQueue.h
* Author: nicolo
*
* Created on January 21, 2020, 1:58 AM
*/
#ifndef SYNCQUEUE_H
#define SYNCQUEUE_H
#include "miosix/kernel/sync.h"
#include <list>
using namespace miosix;
template<typename T>
class SyncQueue {
public:
SyncQueue(){}
void put(const T& data)
{
Lock<Mutex> lck(myMutex);
queue.push_back(data);
myCv.signal();
}
T get()
{
Lock<Mutex> lck(myMutex);
while(queue.empty())
myCv.wait(lck);
T result=queue.front();
queue.pop_front();
return result;
}
size_t size()
{
Lock<Mutex> lck(myMutex);
return queue.size();
}
protected:
std::list<T> queue;
Mutex myMutex;
ConditionVariable myCv;
private:
SyncQueue(const SyncQueue &)=delete;
SyncQueue & operator=(const SyncQueue &)=delete;
};
#endif /* SYNCQUEUE_H */