HOG2
SharedQueue.h
Go to the documentation of this file.
1 //
2 // SharedQueue.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 11/3/14.
6 // Copyright (c) 2014 University of Denver. All rights reserved.
7 //
8 
9 #ifndef SHARED_QUEUE_H
10 #define SHARED_QUEUE_H
11 
12 #include <iostream>
13 #include <mutex>
14 #include <deque>
15 #include <condition_variable>
16 /* SharedQueue
17  *
18  * A producer/consumer queue backed by a deque. Can optionally enforce
19  * capacity constraints. TODO: Need to verify that no deadlocks occur if
20  * mixing waiting and non-waiting functions.
21  */
22 
23 template <typename T>
24 class SharedQueue {
25 public:
26  SharedQueue(int maxCapacity = -1);
27  ~SharedQueue();
28  bool IsEmpty() const;
29  void Add(T value);
30  bool Remove(T &item);
31 
32  void WaitAdd(const T &value);
33  void WaitRemove(T &item);
34  void Print();
35  size_t size();
36 private:
37  std::deque<T> queue;
39  mutable std::mutex lock;
40  mutable std::condition_variable notFull;
41  mutable std::condition_variable notEmpty;
42 };
43 
44 template <typename T>
46 :maxCapacity(capacity)
47 {
48 }
49 
50 template <typename T>
52 {
53 }
54 
55 template <typename T>
57 {
58  lock.lock();
59  bool result = (queue.empty());
60  lock.unlock();
61  return result;
62 }
63 
64 template <typename T>
66 {
67  lock.lock();
68  size_t result = queue.size();
69  lock.unlock();
70  return result;
71 }
72 
73 /* Adds item to queue, regardless of capacity. */
74 template <typename T>
75 void SharedQueue<T>::Add(T value)
76 {
77  lock.lock();
78  queue.push_back(value);
79  lock.unlock();
80  notEmpty.notify_one();
81 }
82 
83 /* Tries to remove, returning true if successful, false otherwise. */
84 template <typename T>
86 {
87  lock.lock();
88  if (queue.empty())
89  {
90  lock.unlock();
91  return false;
92  }
93 
94  item = queue.front();
95  queue.pop_front();
96  lock.unlock();
97  notFull.notify_one();
98  return true;
99 }
100 
101 /* Adds item to queue, waiting if the queue is full. */
102 template <typename T>
103 void SharedQueue<T>::WaitAdd(const T &value)
104 {
105  std::unique_lock<std::mutex> l(lock);
106 
107  notFull.wait(l, [this](){return queue.size() < maxCapacity; });
108  queue.push_back(value);
109 
110  notEmpty.notify_one();
111 }
112 
113 /* Removes item from queue, waiting until item is available. */
114 template <typename T>
116 {
117  std::unique_lock<std::mutex> l(lock);
118 
119  notEmpty.wait(l, [this](){return queue.size() > 0; });
120 
121  item = queue.front();
122  queue.pop_front();
123 
124  notFull.notify_one();
125 }
126 
127 template <typename T>
129 {
130  lock.lock();
131  for (auto c : queue)
132  {
133  std::cout << c << " ";
134  }
135  std::cout << std::endl;
136  lock.unlock();
137 }
138 
139 
140 #endif
SharedQueue::notFull
std::condition_variable notFull
Definition: SharedQueue.h:40
SharedQueue::WaitRemove
void WaitRemove(T &item)
Definition: SharedQueue.h:115
SharedQueue::lock
std::mutex lock
Definition: SharedQueue.h:39
SharedQueue::Print
void Print()
Definition: SharedQueue.h:128
SharedQueue::WaitAdd
void WaitAdd(const T &value)
Definition: SharedQueue.h:103
SharedQueue::SharedQueue
SharedQueue(int maxCapacity=-1)
Definition: SharedQueue.h:45
SharedQueue::queue
std::deque< T > queue
Definition: SharedQueue.h:37
SharedQueue::size
size_t size()
Definition: SharedQueue.h:65
SharedQueue::notEmpty
std::condition_variable notEmpty
Definition: SharedQueue.h:41
SharedQueue::IsEmpty
bool IsEmpty() const
Definition: SharedQueue.h:56
SharedQueue::Add
void Add(T value)
Definition: SharedQueue.h:75
SharedQueue::~SharedQueue
~SharedQueue()
Definition: SharedQueue.h:51
SharedQueue::maxCapacity
int maxCapacity
Definition: SharedQueue.h:38
SharedQueue
Definition: SharedQueue.h:24
SharedQueue::Remove
bool Remove(T &item)
Definition: SharedQueue.h:85