Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _CHANNEL_H_
- #define _CHANNEL_H_
- #include <thread>
- #include <queue>
- #include <mutex>
- #include <condition_variable>
- template <typename T>
- class Channel
- {
- private:
- std::queue<T> m_queue;
- std::mutex m_mutex;
- std::condition_variable m_condition;
- public:
- Channel(void){}
- ~Channel(void){}
- void operator>=(T &item)
- {
- std::unique_lock<std::mutex> mlock(m_mutex);
- while(m_queue.empty())
- {
- m_condition.wait(mlock);
- }
- item = m_queue.front();
- m_queue.pop();
- }
- void operator<=(const T &item)
- {
- std::unique_lock<std::mutex> mlock(m_mutex);
- m_queue.push(item);
- mlock.unlock();
- m_condition.notify_one();
- }
- };
- #endif //#ifndef _CHANNEL_H_
Advertisement
Add Comment
Please, Sign In to add comment