2015-12-08 04:51:55 +08:00
|
|
|
#pragma once
|
2015-10-03 14:07:55 +08:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "threading/sync/lockable.hpp"
|
|
|
|
#include "threading/sync/spinlock.hpp"
|
|
|
|
|
|
|
|
namespace lockfree
|
|
|
|
{
|
|
|
|
|
|
|
|
template <class K, class V>
|
2015-10-04 03:02:51 +08:00
|
|
|
class HashMap: Lockable<SpinLock>
|
|
|
|
{
|
2015-10-03 14:07:55 +08:00
|
|
|
public:
|
2015-10-04 03:02:51 +08:00
|
|
|
|
|
|
|
V at(const K& key)
|
2015-10-03 14:07:55 +08:00
|
|
|
{
|
|
|
|
auto guard = acquire();
|
|
|
|
|
|
|
|
return hashmap[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
void put(const K& key, const K& value)
|
|
|
|
{
|
|
|
|
auto quard = acquire();
|
|
|
|
|
|
|
|
hashmap[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unordered_map<K, V> hashmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|