template<typename Key, typename T, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<std::pair<const Key, T>>>
class dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >
An unordered map supporting synchronised access from multiple threads.
Intended for shared mutable caches where concurrent reads and writes would otherwise cause data races and undefined behaviour.
Inherits from std::unordered_map for API compatibility. Inherited methods are not individually thread-safe; callers must use read() or write() when accessing the map concurrently.
- Template Parameters
-
| Key | Key type |
| T | Mapped value type |
| Hash | Hash function |
| KeyEqual | Key comparison function |
| Allocator | Allocator type |
template<typename Key , typename T , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<std::pair<const Key, T>>>
Safely retrieve a copy of a value by key.
Returns a snapshot of the mapped value while holding a shared lock. Modifying the returned value does not modify the map.
- Parameters
-
- Returns
- A copy of the value, or std::nullopt if the key does not exist
template<typename Key , typename T , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<std::pair<const Key, T>>>
template<typename F >
Safely access the map for reading.
Allows concurrent readers while preventing modification. Do not retain references or iterators beyond the callback.
- Template Parameters
-
- Parameters
-
| fn | Function receiving a const reference to the map |
- Returns
- The result of the callable
template<typename Key , typename T , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<std::pair<const Key, T>>>
template<typename F >
Safely access the map for modification.
Provides exclusive access for compound operations. Do not dispatch events, queue work or invoke user callbacks while holding the lock.
- Template Parameters
-
- Parameters
-
| fn | Function receiving a mutable reference to the map |
- Returns
- The result of the callable