D++ (DPP)
C++ Discord API Bot Library
dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator > Class Template Reference

An unordered map supporting synchronised access from multiple threads. More...

#include <thread_safe_unordered_map.h>

+ Inheritance diagram for dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >:
+ Collaboration diagram for dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >:

Public Member Functions

 thread_safe_unordered_map ()=default
 Construct an empty thread-safe unordered map. More...
 
 thread_safe_unordered_map (const thread_safe_unordered_map &other)
 Copy construct a thread-safe unordered map. More...
 
 thread_safe_unordered_map (thread_safe_unordered_map &&other)
 Move construct a thread-safe unordered map. More...
 
std::optional< T > get (const Key &key) const
 Safely retrieve a copy of a value by key. More...
 
thread_safe_unordered_map & operator= (const thread_safe_unordered_map &other)
 Copy assign a thread-safe unordered map. More...
 
thread_safe_unordered_map & operator= (thread_safe_unordered_map &&other)
 Move assign a thread-safe unordered map. More...
 
template<typename F >
decltype(auto) read (F &&fn) const
 Safely access the map for reading. More...
 
template<typename F >
decltype(auto) write (F &&fn)
 Safely access the map for modification. More...
 

Detailed Description

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
KeyKey type
TMapped value type
HashHash function
KeyEqualKey comparison function
AllocatorAllocator type

Constructor & Destructor Documentation

◆ thread_safe_unordered_map() [1/3]

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>>>
dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >::thread_safe_unordered_map ( )
default

Construct an empty thread-safe unordered map.

◆ thread_safe_unordered_map() [2/3]

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>>>
dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >::thread_safe_unordered_map ( const thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator > &  other)
inline

Copy construct a thread-safe unordered map.

Parameters
otherMap to copy

◆ thread_safe_unordered_map() [3/3]

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>>>
dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >::thread_safe_unordered_map ( thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator > &&  other)
inline

Move construct a thread-safe unordered map.

Parameters
otherMap to move from

Member Function Documentation

◆ get()

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>>>
std::optional< T > dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >::get ( const Key &  key) const
inline

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
keyKey to look up
Returns
A copy of the value, or std::nullopt if the key does not exist

◆ operator=() [1/2]

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>>>
thread_safe_unordered_map & dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >::operator= ( const thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator > &  other)
inline

Copy assign a thread-safe unordered map.

Parameters
otherMap to copy
Returns
Reference to this map

◆ operator=() [2/2]

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>>>
thread_safe_unordered_map & dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >::operator= ( thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator > &&  other)
inline

Move assign a thread-safe unordered map.

Parameters
otherMap to move from
Returns
Reference to this map

◆ read()

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 >
decltype(auto) dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >::read ( F &&  fn) const
inline

Safely access the map for reading.

Allows concurrent readers while preventing modification. Do not retain references or iterators beyond the callback.

Template Parameters
FCallable type
Parameters
fnFunction receiving a const reference to the map
Returns
The result of the callable

◆ write()

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 >
decltype(auto) dpp::thread_safe_unordered_map< Key, T, Hash, KeyEqual, Allocator >::write ( F &&  fn)
inline

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
FCallable type
Parameters
fnFunction receiving a mutable reference to the map
Returns
The result of the callable
D++ Library version 10.1.7D++ Library version 10.1.6D++ Library version 10.1.5D++ Library version 10.1.4D++ Library version 10.1.3D++ Library version 10.1.2D++ Library version 10.1.1D++ Library version 10.1.0D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0