提交 c1d54d47 编写于 作者: 独孤过's avatar 独孤过

Update Queue

上级 8c517af2
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
作者:许聪 作者:许聪
邮箱:2592419242@qq.com 邮箱:2592419242@qq.com
创建日期:2019年03月08日 创建日期:2019年03月08日
更新日期:2022年01月22 更新日期:2022年01月29
变化: 变化:
v1.5 v1.5
...@@ -109,29 +109,27 @@ std::optional<typename Queue<_ElementType>::SizeType> Queue<_ElementType>::push( ...@@ -109,29 +109,27 @@ std::optional<typename Queue<_ElementType>::SizeType> Queue<_ElementType>::push(
template <typename _ElementType> template <typename _ElementType>
std::optional<typename Queue<_ElementType>::SizeType> Queue<_ElementType>::push(QueueType& _queue) std::optional<typename Queue<_ElementType>::SizeType> Queue<_ElementType>::push(QueueType& _queue)
{ {
auto length = _queue.size();
std::lock_guard lock(_entryMutex); std::lock_guard lock(_entryMutex);
if (auto size = this->size(); \ if (auto size = this->size(); \
_capacity > 0 && (size >= _capacity || length >= _capacity - size)) _capacity > 0 && (size >= _capacity || _queue.size() >= _capacity - size))
return std::nullopt; return std::nullopt;
auto size = _queue.size();
_entryQueue.splice(_entryQueue.cend(), _queue); _entryQueue.splice(_entryQueue.cend(), _queue);
return add(length); return add(size);
} }
template <typename _ElementType> template <typename _ElementType>
std::optional<typename Queue<_ElementType>::SizeType> Queue<_ElementType>::push(QueueType&& _queue) std::optional<typename Queue<_ElementType>::SizeType> Queue<_ElementType>::push(QueueType&& _queue)
{ {
auto length = _queue.size();
std::lock_guard lock(_entryMutex); std::lock_guard lock(_entryMutex);
if (auto size = this->size(); \ if (auto size = this->size(); \
_capacity > 0 && (size >= _capacity || length >= _capacity - size)) _capacity > 0 && (size >= _capacity || _queue.size() >= _capacity - size))
return std::nullopt; return std::nullopt;
auto size = _queue.size();
_entryQueue.splice(_entryQueue.cend(), _queue); _entryQueue.splice(_entryQueue.cend(), _queue);
return add(length); return add(size);
} }
template <typename _ElementType> template <typename _ElementType>
...@@ -171,10 +169,8 @@ bool Queue<_ElementType>::pop(QueueType& _queue) ...@@ -171,10 +169,8 @@ bool Queue<_ElementType>::pop(QueueType& _queue)
template <typename _ElementType> template <typename _ElementType>
void Queue<_ElementType>::clear() void Queue<_ElementType>::clear()
{ {
std::lock_guard exitLock(_exitMutex); std::scoped_lock lock(_exitMutex, _entryMutex);
_exitQueue.clear(); _exitQueue.clear();
std::lock_guard entryLock(_entryMutex);
_entryQueue.clear(); _entryQueue.clear();
set(0); set(0);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册