Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
hot_path_helper.h File Reference

Reusable hot-path optimization patterns for concurrent map access. More...

#include <shared_mutex>
Include dependency graph for hot_path_helper.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  kcenon
 
namespace  kcenon::monitoring
 
namespace  kcenon::monitoring::hot_path
 

Functions

template<typename Map , typename Key , typename CreateFn >
auto kcenon::monitoring::hot_path::get_or_create (Map &map, std::shared_mutex &mutex, const Key &key, CreateFn create_fn) -> typename std::remove_reference< decltype(*map.begin() ->second)>::type *
 
template<typename Map , typename Key , typename CreateFn , typename InitFn >
auto kcenon::monitoring::hot_path::get_or_create_with_init (Map &map, std::shared_mutex &mutex, const Key &key, CreateFn create_fn, InitFn init_fn) -> typename std::remove_reference< decltype(*map.begin() ->second)>::type *
 
template<typename Map , typename Key , typename CreateFn , typename UpdateFn >
auto kcenon::monitoring::hot_path::get_or_create_and_update (Map &map, std::shared_mutex &mutex, const Key &key, CreateFn create_fn, UpdateFn update_fn) -> decltype(update_fn(*map.begin() ->second))
 

Detailed Description

Reusable hot-path optimization patterns for concurrent map access.

Date
2025

Provides thread-safe get-or-create patterns using double-check locking with shared_mutex for optimal read performance on hot paths.

The pattern reduces lock contention by:

  1. First attempting read with shared_lock (allows concurrent readers)
  2. Only acquiring exclusive write_lock when creation is necessary
  3. Double-checking after write_lock to handle race conditions

Definition in file hot_path_helper.h.