/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef INCLUDED_TSQUEUE_H #define INCLUDED_TSQUEUE_H #include "config.h" #include #include #include // Thread-safe queue template class tsqueue { public: void put(const T& value) { std::unique_lock lock(_mutex); _queue.push_back(value); lock.unlock(); _cv.notify_one(); } T get() { std::unique_lock lock(_mutex); _cv.wait(lock, [this] { return _queue.size() > 0; }); T result = _queue.front(); _queue.pop_front(); return result; } void clear() { std::unique_lock lock(_mutex); while (_queue.size()) _queue.pop_front(); } template void remove_if(UnaryPredicate p) { std::unique_lock lock(_mutex); _queue.erase(std::remove_if(_queue.begin(), _queue.end(), p), _queue.end()); } bool alreadyInQueue(std::string cmd) { for (auto it = _queue.cbegin(); it != _queue.cend(); ++it) { if (cmd == *it) { return true; } } return false; } private: std::mutex _mutex; std::condition_variable _cv; std::deque _queue; }; #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */