/* * BlockingQueue.h, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #pragma once #include "StdInc.h" VCMI_LIB_NAMESPACE_BEGIN //Credit to https://github.com/Liam0205/toy-threadpool/tree/master/yuuki template class DLL_LINKAGE BlockingQueue : protected std::queue { using WriteLock = boost::unique_lock; using Readlock = boost::shared_lock; public: BlockingQueue() = default; ~BlockingQueue() { clear(); } BlockingQueue(const BlockingQueue&) = delete; BlockingQueue(BlockingQueue&&) = delete; BlockingQueue& operator=(const BlockingQueue&) = delete; BlockingQueue& operator=(BlockingQueue&&) = delete; public: bool empty() const { Readlock lock(mx); return std::queue::empty(); } size_t size() const { Readlock lock(mx); return std::queue::size(); } public: void clear() { WriteLock lock(mx); while (!std::queue::empty()) { std::queue::pop(); } } void push(const T& obj) { WriteLock lock(mx); std::queue::push(obj); } template void emplace(Args&&... args) { WriteLock lock(mx); std::queue::emplace(std::forward(args)...); } bool pop(T& holder) { WriteLock lock(mx); if (std::queue::empty()) { return false; } else { holder = std::move(std::queue::front()); std::queue::pop(); return true; } } private: mutable boost::shared_mutex mx; }; VCMI_LIB_NAMESPACE_END