2020#include < deque>
2121#include < map>
2222#include < mutex>
23+ #include < shared_mutex>
2324#include < set>
2425#include < unordered_map>
2526#include < unordered_set>
@@ -222,14 +223,14 @@ class CacheMap
222223 if (timeout > 0 )
223224 {
224225 MapValue v{std::move (value), timeout, std::move (timeoutCallback)};
225- std::lock_guard <std::mutex > lock (mtx_);
226+ std::unique_lock <std::shared_mutex > lock (mtx_);
226227 map_.insert (std::make_pair (key, std::move (v)));
227228 eraseAfter (timeout, key);
228229 }
229230 else
230231 {
231232 MapValue v{std::move (value)};
232- std::lock_guard <std::mutex > lock (mtx_);
233+ std::unique_lock <std::shared_mutex > lock (mtx_);
233234 map_.insert (std::make_pair (key, std::move (v)));
234235 }
235236 if (fnOnInsert_)
@@ -254,14 +255,14 @@ class CacheMap
254255 if (timeout > 0 )
255256 {
256257 MapValue v{value, timeout, std::move (timeoutCallback)};
257- std::lock_guard <std::mutex > lock (mtx_);
258+ std::unique_lock <std::shared_mutex > lock (mtx_);
258259 map_.insert (std::make_pair (key, std::move (v)));
259260 eraseAfter (timeout, key);
260261 }
261262 else
262263 {
263264 MapValue v{value};
264- std::lock_guard <std::mutex > lock (mtx_);
265+ std::unique_lock <std::shared_mutex > lock (mtx_);
265266 map_.insert (std::make_pair (key, std::move (v)));
266267 }
267268 if (fnOnInsert_)
@@ -280,7 +281,7 @@ class CacheMap
280281 T2 operator [](const T1 &key)
281282 {
282283 size_t timeout = 0 ;
283- std::lock_guard <std::mutex > lock (mtx_);
284+ std::shared_lock <std::shared_mutex > lock (mtx_);
284285 auto iter = map_.find (key);
285286 if (iter != map_.end ())
286287 {
@@ -312,7 +313,7 @@ class CacheMap
312313 void modify (const T1 &key, Callable &&handler, size_t timeout = 0 )
313314 {
314315 {
315- std::lock_guard <std::mutex > lock (mtx_);
316+ std::unique_lock <std::shared_mutex > lock (mtx_);
316317 auto iter = map_.find (key);
317318 if (iter != map_.end ())
318319 {
@@ -341,7 +342,8 @@ class CacheMap
341342 size_t timeout = 0 ;
342343 bool flag = false ;
343344
344- std::lock_guard<std::mutex> lock (mtx_);
345+ // Using shared_lock to allow others thread as well at the same time
346+ std::shared_lock<std::shared_mutex> lock (mtx_);
345347 auto iter = map_.find (key);
346348 if (iter != map_.end ())
347349 {
@@ -364,7 +366,7 @@ class CacheMap
364366 {
365367 size_t timeout = 0 ;
366368 bool flag = false ;
367- std::lock_guard <std::mutex > lock (mtx_);
369+ std::shared_lock <std::shared_mutex > lock (mtx_);
368370 auto iter = map_.find (key);
369371 if (iter != map_.end ())
370372 {
@@ -388,7 +390,7 @@ class CacheMap
388390 {
389391 // in this case,we don't evoke the timeout callback;
390392 {
391- std::lock_guard <std::mutex > lock (mtx_);
393+ std::unique_lock <std::shared_mutex > lock (mtx_);
392394 map_.erase (key);
393395 }
394396 if (fnOnErase_)
@@ -453,7 +455,7 @@ class CacheMap
453455
454456 std::atomic<size_t > ticksCounter_{0 };
455457
456- std::mutex mtx_;
458+ std::shared_mutex mtx_;
457459 std::mutex bucketMutex_;
458460 trantor::TimerId timerId_;
459461 trantor::EventLoop *loop_;
@@ -529,7 +531,7 @@ class CacheMap
529531 bool erased{false };
530532 std::function<void ()> timeoutCallback;
531533 {
532- std::lock_guard <std::mutex > lock (mtx_);
534+ std::unique_lock <std::shared_mutex > lock (mtx_);
533535 auto iter = map_.find (key);
534536 if (iter != map_.end ())
535537 {
0 commit comments