From b4529532dce8bc2d1ad3dc5f69098d4c3730d3de Mon Sep 17 00:00:00 2001 From: ruanshudong Date: Mon, 6 Apr 2020 21:30:19 +0800 Subject: [PATCH] tc_thread queue add front & pop_front --- util/include/util/tc_cas_queue.h | 2 +- util/include/util/tc_thread_queue.h | 37 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/util/include/util/tc_cas_queue.h b/util/include/util/tc_cas_queue.h index 6d6bc8b..b475acd 100755 --- a/util/include/util/tc_cas_queue.h +++ b/util/include/util/tc_cas_queue.h @@ -34,7 +34,7 @@ public: typedef D queue_type; /** - * @brief 从头部获取数据, 没有数据则等待. + * @brief 从头部获取数据, 没有数据抛异常 * * @param t * @return bool: true, 获取了数据, false, 无数据 diff --git a/util/include/util/tc_thread_queue.h b/util/include/util/tc_thread_queue.h index 9072712..edfc7d6 100644 --- a/util/include/util/tc_thread_queue.h +++ b/util/include/util/tc_thread_queue.h @@ -48,6 +48,14 @@ public: typedef D queue_type; + /** + * @brief 从头部获取数据, 没有数据抛异常 + * + * @param t + * @return bool: true, 获取了数据, false, 无数据 + */ + T front(); + /** * @brief 从头部获取数据, 没有数据则等待. * @@ -60,6 +68,13 @@ public: */ bool pop_front(T& t, size_t millsecond = 0, bool wait = true); + /** + * @brief 从头部获取数据. + * + * @return bool: true, 获取了数据, false, 无数据 + */ + bool pop_front(); + /** * @brief 通知等待在队列上面的线程都醒过来 */ @@ -148,6 +163,13 @@ protected: mutable std::mutex _mutex; }; +template T TC_ThreadQueue::front() +{ + std::unique_lock lock(_mutex); + + return _queue.front(); +} + template bool TC_ThreadQueue::pop_front(T& t, size_t millsecond, bool wait) { if(wait) { @@ -198,6 +220,21 @@ template bool TC_ThreadQueue::pop_front(T& t, size } +template bool TC_ThreadQueue::pop_front() +{ + std::unique_lock lock(_mutex); + if (_queue.empty()) + { + return false; + } + + _queue.pop_front(); + assert(_size > 0); + --_size; + + return true; +} + template void TC_ThreadQueue::notifyT() { std::unique_lock lock(_mutex);