mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-26 02:21:34 +08:00
blocking queue to notify one and not all
This commit is contained in:
parent
be9dd21fbf
commit
e88a46691a
@ -68,22 +68,19 @@ public:
|
|||||||
// Return: false on timeout, true on successful push.
|
// Return: false on timeout, true on successful push.
|
||||||
template<typename Duration_Rep, typename Duration_Period, typename TT>
|
template<typename Duration_Rep, typename Duration_Period, typename TT>
|
||||||
bool push(TT&& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout)
|
bool push(TT&& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout)
|
||||||
|
{
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> ul(_mutex);
|
std::unique_lock<std::mutex> ul(_mutex);
|
||||||
if (_q.size() >= _max_size)
|
if (is_full())
|
||||||
{
|
{
|
||||||
if (!_item_popped_cond.wait_until(ul, clock::now() + timeout,[this]()
|
if (!_item_popped_cond.wait_until(ul, clock::now() + timeout, [this]() {
|
||||||
{
|
return !this->is_full();
|
||||||
return this->_q.size() < this->_max_size;
|
|
||||||
}))
|
}))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
_q.push(std::forward<TT>(item));
|
_q.push(std::forward<TT>(item));
|
||||||
if (_q.size() <= 1)
|
|
||||||
{
|
|
||||||
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
|
|
||||||
_item_pushed_cond.notify_all();
|
|
||||||
}
|
}
|
||||||
|
_item_pushed_cond.notify_one();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +89,7 @@ public:
|
|||||||
template<typename TT>
|
template<typename TT>
|
||||||
void push(TT&& item)
|
void push(TT&& item)
|
||||||
{
|
{
|
||||||
while (!push(std::forward<TT>(item), std::chrono::seconds(10)));
|
while (!push(std::forward<TT>(item), std::chrono::seconds(60)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop a copy of the front item in the queue into the given item ref.
|
// Pop a copy of the front item in the queue into the given item ref.
|
||||||
@ -100,23 +97,21 @@ public:
|
|||||||
// Return: false on timeout , true on successful pop/
|
// Return: false on timeout , true on successful pop/
|
||||||
template<class Duration_Rep, class Duration_Period>
|
template<class Duration_Rep, class Duration_Period>
|
||||||
bool pop(T& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout)
|
bool pop(T& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout)
|
||||||
|
{
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> ul(_mutex);
|
std::unique_lock<std::mutex> ul(_mutex);
|
||||||
if (_q.empty())
|
if (is_empty())
|
||||||
{
|
{
|
||||||
if (!_item_pushed_cond.wait_until(ul, clock::now() + timeout, [this]()
|
if (!_item_pushed_cond.wait_until(ul, clock::now() + timeout, [this]()
|
||||||
{
|
{
|
||||||
return !this->_q.empty();
|
return !this->is_empty();
|
||||||
}))
|
}))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
item = std::move(_q.front());
|
item = std::move(_q.front());
|
||||||
_q.pop();
|
_q.pop();
|
||||||
if (_q.size() >= _max_size - 1)
|
|
||||||
{
|
|
||||||
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
|
|
||||||
_item_popped_cond.notify_all();
|
|
||||||
}
|
}
|
||||||
|
_item_popped_cond.notify_one();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,6 +138,16 @@ private:
|
|||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
std::condition_variable _item_pushed_cond;
|
std::condition_variable _item_pushed_cond;
|
||||||
std::condition_variable _item_popped_cond;
|
std::condition_variable _item_popped_cond;
|
||||||
|
|
||||||
|
inline bool is_full()
|
||||||
|
{
|
||||||
|
return _q.size() >= _max_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool is_empty()
|
||||||
|
{
|
||||||
|
return _q.size() == 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user