| 1 |
#define THIS_HASH_MAP parallel_flat_hash_map |
| 2 |
#define THIS_TEST_NAME ParallelFlatHashMap |
| 3 |
#include <thread> |
| 4 |
|
| 5 |
#include "parallel_hash_map_test.cc" |
| 6 |
|
| 7 |
|
| 8 |
#if PHMAP_HAVE_SHARED_MUTEX |
| 9 |
#include <shared_mutex> |
| 10 |
|
| 11 |
template <typename K> using HashEqual = phmap::priv::hash_default_eq<K>; |
| 12 |
template <typename V> using HashFn = phmap::priv::hash_default_hash<V>; |
| 13 |
template <typename K> using Allocator = phmap::priv::Allocator<K>; |
| 14 |
|
| 15 |
template <typename K, typename V, size_t N> |
| 16 |
using parallel_flat_hash_map = |
| 17 |
phmap::parallel_flat_hash_map<K, V, HashFn<K>, HashEqual<K>, |
| 18 |
Allocator<phmap::priv::Pair<K, V>>, N, |
| 19 |
std::shared_mutex>; |
| 20 |
|
| 21 |
using Table = parallel_flat_hash_map<int, int, 10>; |
| 22 |
|
| 23 |
TEST(THIS_TEST_NAME, ConcurrencyCheck) { |
| 24 |
static constexpr int THREADS = 10; |
| 25 |
static constexpr int EPOCH = 1000; |
| 26 |
static constexpr int KEY = 12345; |
| 27 |
|
| 28 |
auto Incr = [](Table *table) { |
| 29 |
auto exist_fn = [](typename Table::value_type &value) { value.second += 1; }; |
| 30 |
auto emplace_fn = [](const typename Table::constructor &ctor) { |
| 31 |
ctor(KEY, 1); |
| 32 |
}; |
| 33 |
for (int i = 0; i < EPOCH; ++i) { |
| 34 |
(void)table->lazy_emplace_l(KEY, exist_fn, emplace_fn); |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
Table table; |
| 39 |
std::vector<std::thread> threads; |
| 40 |
threads.reserve(THREADS); |
| 41 |
for (int i = 0; i < THREADS; ++i) { |
| 42 |
threads.emplace_back([&]() { Incr(&table); }); |
| 43 |
} |
| 44 |
|
| 45 |
for (auto &thread : threads) { |
| 46 |
thread.join(); |
| 47 |
} |
| 48 |
|
| 49 |
EXPECT_EQ(table[KEY], 10000); |
| 50 |
} |
| 51 |
|
| 52 |
#endif |