Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
concurrent_hash_map_example.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
39#include <print_rule.H>
41
42#include <atomic>
43#include <iostream>
44#include <thread>
45#include <vector>
46
47using namespace Aleph;
48
49namespace
50{
52{
53 std::cout << "[1] Single-threaded insert/find/erase walkthrough\n";
54 print_rule();
55
57 counters.insert("visits", 0);
58 counters.insert("errors", 0);
59
60 counters.with_value_mut("visits", [](int &v) { v += 3; });
61 counters.insert_or_assign("errors", 1);
62
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";
67}
68
70{
71 std::cout << "[2] Eight worker threads sharing one sharded map\n";
72 print_rule();
73
74 constexpr int worker_count = 8;
75 constexpr int keys_per_worker = 4000;
76
77 // 16 shards: each worker's keys are spread across shards by hash, so
78 // most concurrent inserts from different workers proceed without
79 // contending on the same shard lock.
81 std::atomic<bool> start{false};
82 std::vector<std::thread> workers;
83
84 for (int w = 0; w < worker_count; ++w)
85 workers.emplace_back([&, w]
86 {
87 while (not start.load(std::memory_order_acquire))
88 ; // wait until every worker thread has been created
89 const int base = w * keys_per_worker;
90 for (int i = 0; i < keys_per_worker; ++i)
91 map.insert(base + i, i);
92 });
93
94 start.store(true, std::memory_order_release);
95 for (auto &w : workers)
96 w.join();
97
98 std::cout << "Total entries: " << map.size() << " (expect "
99 << worker_count * keys_per_worker << ")\n";
100
101 auto snapshot = map.snapshot();
102 std::cout << "Snapshot size: " << snapshot.size()
103 << " (an independent copy, unaffected by later mutations)\n\n";
104}
105} // namespace
106
107int main()
108{
109 std::cout << "\n=== Aleph::ConcurrentHashMap: sharded concurrent map ===\n\n";
110
113
114 std::cout << "Done.\n";
115 return 0;
116}
long double w
Definition btreepic.C:153
Sharded concurrent hash map: Shards independently-locked DynMapHashTable partitions,...
size_t size() const
Return the total number of entries across all shards.
bool insert(const Key &key, const T &value)
Insert key with a copy of value, only if key is absent.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
Sharded concurrent hash map (Aleph::ConcurrentHashMap).