/*** Created by Liam Huang (Liam0205) on 2018年10月11日.*/#ifndef QUEUE_CONCURRENCY_QUEUE_HPP_#define QUEUE_CONCURRENCY_QUEUE_HPP_#include <queue>#include <mutex>#include <condition_variable>#include <memory>template <typename T>class ConcurrencyQueue {public:using value_type = T;using container_type = std::queue<value_type>;using size_type = typename container_type::size_type;private:container_type container_;mutable std::mutex mutex_;std::condition_variable container_cond_;public:ConcurrencyQueue() = default;ConcurrencyQueue(const ConcurrencyQueue&) = default;ConcurrencyQueue(ConcurrencyQueue&&) = default;ConcurrencyQueue& operator=(const ConcurrencyQueue&) = default;ConcurrencyQueue& operator=(ConcurrencyQueue&&) = default;private:bool empty_() const { return container_.empty(); }public:bool empty() const {std::lock_guard<std::mutex> lg(mutex_);return container_.empty();}void push(value_type item) {std::lock_guard<std::mutex> lg(mutex_);container_.push(std::move(item));container_cond_.notify_one();}void wait_and_pop(value_type& out) {std::unique_lock<std::mutex> lk(mutex_);while (empty_()) {container_cond_.wait(lk)}out = std::move(container_.front());container_.pop();}std::shared_ptr<value_type> wait_and_pop() {std::unique_lock<std::mutex> lk(mutex_);while (empty_()) {container_cond_.wait(lk)}auto res = std::make_shared<value_type>(std::move(container_.front()));container_.pop();return res;}bool try_pop(value_type& out) {std::lock_guard<std::mutex> lg(mutex_);if (empty_()) {return false;} else {out = std::move(container_.front());container_.pop();return true;}}std::shared_ptr<value_type> try_pop() {std::lock_guard<std::mutex> lg(mutex_);if (empty_()) {return nullptr;} else {auto res = std::make_shared<value_type>(std::move(container_.front()));container_.pop();return res;}}};#endif // QUEUE_CONCURRENCY_QUEUE_HPP_
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。