// // sync_queue.h // adapted from an example from Bjarne Stroustrup // #ifndef __sync_queue__ #define __sync_queue__ #include #include #include #include #include namespace sc { template class sync_queue { public: void put(const T& val) { std::lock_guard lck {mtx}; q.push_back(val); cond.notify_one(); } T get() { std::unique_lock lck {mtx}; cond.wait(lck, [this]{ return !q.empty(); }); T val {q.front()}; q.pop_front(); return val; } private: std::mutex mtx; std::condition_variable cond; std::list q; }; } #endif /* defined(__sync_queue__) */