Browse Source

Added the queue src and update Documentation

Carl Olsen 5 years ago
parent
commit
37fb42b678
3 changed files with 61 additions and 0 deletions
  1. 6 0
      README.md
  2. 4 0
      doc/Doc.org
  3. 51 0
      src/Queue.hpp

+ 6 - 0
README.md

@@ -5,6 +5,7 @@
 <li><a href="#sec-1">1. Yas-Threads</a></li>
 <li><a href="#sec-2">2. Extra functionality</a></li>
 <li><a href="#sec-3">3. Using more msgPump-snippets</a></li>
+<li><a href="#sec-4">4. Getting the Queue</a></li>
 </ul>
 </div>
 </div>
@@ -103,3 +104,8 @@ exists.
 
 One note, if the default names for the queue and thread are not used,
 these needs to be changed by hand during the above snippets.
+
+# Getting the Queue<a id="sec-4" name="sec-4"></a>
+
+Since the queue is basically the same for all, i've added the file
+in [Queue](./src/Queue.hpp). Feel free to use it.

+ 4 - 0
doc/Doc.org

@@ -96,3 +96,7 @@ exists.
 One note, if the default names for the queue and thread are not used,
 these needs to be changed by hand during the above snippets.
 		    
+* Getting the Queue
+Since the queue is basically the same for all, i've added the file
+in [[./src/Queue.hpp][Queue]]. Feel free to use it.
+

+ 51 - 0
src/Queue.hpp

@@ -0,0 +1,51 @@
+#ifndef QUEUE_H
+#define QUEUE_H
+
+#include <mutex>
+#include <queue>
+#include <condition_variable>
+
+namespace Util {
+  
+  using std::queue;
+
+
+  template< class T >
+  class Queue
+  {
+  public:
+    Queue() {}
+    virtual ~Queue() {}
+      
+    
+    void push(T message)
+    {
+      std::lock_guard< std::mutex > lock(mutex_);
+      itsQueue_.push(message);
+      notifier_.notify_one();
+    }
+      
+
+    void pop(T& message) 
+    {
+      std::unique_lock< std::mutex > lock(mutex_);
+      //predicate which returns ​false if the waiting should be continued. 
+      //So waiting for something to be added to the queue..
+      notifier_.wait(lock, [this](){ return !itsQueue_.empty(); }); 
+      message = itsQueue_.front();
+      itsQueue_.pop();
+
+
+    }
+    
+  private:
+    queue<T> itsQueue_;
+    mutable std::mutex mutex_;
+    std::condition_variable notifier_;  
+  };
+
+  
+  
+}  // Util
+
+#endif /* QUEUE_H */