10#include <unordered_map>
17namespace traffic_manager {
27 template <
typename Key,
typename Value>
40 std::unordered_map<Key, Value>
map;
54 void AddEntry(
const std::pair<Key, Value> &entry) {
56 std::lock_guard<std::mutex> lock(
map_mutex);
57 const Key& key = entry.first;
58 if (
map.find(key) !=
map.end()) {
59 map.at(key) = entry.second;
72 std::lock_guard<std::mutex> lock(
map_mutex);
73 return map.find(key) !=
map.end();
83 std::lock_guard<std::mutex> lock(
map_mutex);
93 std::lock_guard<std::mutex> lock(
map_mutex);
一个线程安全的、基于unordered_map的原子映射类。
std::unordered_map< Key, Value > map
存储键值对的无序映射。
void RemoveEntry(const Key &key)
从映射中移除指定的键及其对应的值。
const Value & GetValue(const Key &key) const
获取指定键的值。
bool Contains(const Key &key) const
检查映射是否包含指定的键。
void AddEntry(const std::pair< Key, Value > &entry)
添加或更新键值对。
std::mutex map_mutex
用于同步对map的访问的可变互斥锁。