Guest User

Untitled

a guest
May 14th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #ifndef _CHANNEL_H_
  2. #define _CHANNEL_H_
  3. #include <thread>
  4. #include <queue>
  5. #include <mutex>
  6. #include <condition_variable>
  7.  
  8. template <typename T>
  9. class Channel
  10. {
  11. private:
  12. std::queue<T> m_queue;
  13. std::mutex m_mutex;
  14. std::condition_variable m_condition;
  15.  
  16. public:
  17. Channel(void){}
  18. ~Channel(void){}
  19.  
  20. void operator>=(T &item)
  21. {
  22. std::unique_lock<std::mutex> mlock(m_mutex);
  23. while(m_queue.empty())
  24. {
  25. m_condition.wait(mlock);
  26. }
  27. item = m_queue.front();
  28. m_queue.pop();
  29. }
  30.  
  31. void operator<=(const T &item)
  32. {
  33. std::unique_lock<std::mutex> mlock(m_mutex);
  34. m_queue.push(item);
  35. mlock.unlock();
  36. m_condition.notify_one();
  37. }
  38. };
  39.  
  40. #endif //#ifndef _CHANNEL_H_
Advertisement
Add Comment
Please, Sign In to add comment