修复loop,增加loop2

This commit is contained in:
NH
2025-08-19 21:09:39 +08:00
parent f599d4b31d
commit 06c684c090

View File

@@ -138,14 +138,27 @@ namespace ylib
}
return false;
}
void loop(std::function<void(KEY& key,VAL& value)> callback, bool lock = true)
void loop(std::function<void(const KEY& key, const VAL& value)> callback)
{
if (lock)
m_mutex.lock();
std::unique_lock<std::mutex> __guard_lock__(m_mutex);
for_iter(iter, (*this))
{
callback(iter->first, iter->second);
}
}
void loop2(std::function<bool(const KEY& key, VAL& value)> callback, bool lock = true)
{
if (lock)
m_mutex.lock();
for (auto iter = this->begin(); iter != this->end(); ++iter)
{
if (callback(iter->first, iter->second) == false)
{
if (lock)
m_mutex.unlock();
return;
}
}
if (lock)
m_mutex.unlock();
}