tc_thread queue add front & pop_front

This commit is contained in:
ruanshudong 2020-04-06 21:30:19 +08:00
parent 94f8cf8d18
commit b4529532dc
2 changed files with 38 additions and 1 deletions

View File

@ -34,7 +34,7 @@ public:
typedef D queue_type;
/**
* @brief , .
* @brief ,
*
* @param t
* @return bool: true, , false,

View File

@ -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<typename T, typename D> T TC_ThreadQueue<T, D>::front()
{
std::unique_lock<std::mutex> lock(_mutex);
return _queue.front();
}
template<typename T, typename D> bool TC_ThreadQueue<T, D>::pop_front(T& t, size_t millsecond, bool wait)
{
if(wait) {
@ -198,6 +220,21 @@ template<typename T, typename D> bool TC_ThreadQueue<T, D>::pop_front(T& t, size
}
template<typename T, typename D> bool TC_ThreadQueue<T, D>::pop_front()
{
std::unique_lock<std::mutex> lock(_mutex);
if (_queue.empty())
{
return false;
}
_queue.pop_front();
assert(_size > 0);
--_size;
return true;
}
template<typename T, typename D> void TC_ThreadQueue<T, D>::notifyT()
{
std::unique_lock<std::mutex> lock(_mutex);