51void demo_basic_usage()
53 std::cout <<
"[1] Single-threaded insert/find/erase walkthrough\n";
57 counters.
insert(
"visits", 0);
58 counters.
insert(
"errors", 0);
63 std::cout <<
"visits: " << *counters.
find_copy(
"visits") <<
" (expect 3)\n";
64 std::cout <<
"errors: " << *counters.
find_copy(
"errors") <<
" (expect 1)\n";
65 std::cout <<
"contains 'timeouts': " << std::boolalpha
66 << counters.
contains(
"timeouts") <<
" (expect false)\n\n";
69void demo_concurrent_workers()
71 std::cout <<
"[2] Eight worker threads sharing one sharded map\n";
74 constexpr int worker_count = 8;
75 constexpr int keys_per_worker = 4000;
81 std::atomic<bool> start{
false};
82 std::vector<std::thread> workers;
84 for (
int w = 0;
w < worker_count; ++
w)
85 workers.emplace_back([&,
w]
87 while (not start.load(std::memory_order_acquire))
89 const int base = w * keys_per_worker;
90 for (int i = 0; i < keys_per_worker; ++i)
91 map.insert(base + i, i);
94 start.store(
true, std::memory_order_release);
95 for (
auto &
w : workers)
98 std::cout <<
"Total entries: " << map.
size() <<
" (expect "
99 << worker_count * keys_per_worker <<
")\n";
102 std::cout <<
"Snapshot size: " <<
snapshot.size()
103 <<
" (an independent copy, unaffected by later mutations)\n\n";
109 std::cout <<
"\n=== Aleph::ConcurrentHashMap: sharded concurrent map ===\n\n";
112 demo_concurrent_workers();
114 std::cout <<
"Done.\n";
Sharded concurrent hash map: Shards independently-locked DynMapHashTable partitions,...
bool contains(const Key &key) const
Check whether key is present.
void insert_or_assign(const Key &key, T value)
Insert key with value, or overwrite the existing value if key is already present.
bool with_value_mut(const Key &key, F &&f)
Invoke f with a mutable reference to key's value, while the owning shard is write-locked.
size_t size() const
Return the total number of entries across all shards.
std::optional< T > find_copy(const Key &key) const
Look up key and return a copy of its value, if present.
bool insert(const Key &key, const T &value)
Insert key with a copy of value, only if key is absent.
Main namespace for Aleph-w library functions.
void print_rule()
Prints a horizontal rule for example output separation.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
Snapshot snapshot(const Aleph::Adaptive_Huffman_Encoder &encoder)
Reconstruct and validate the tree using public encoder operations.
Sharded concurrent hash map (Aleph::ConcurrentHashMap).