80 log_module::write_information(
"\n=== Basic Hazard Pointer Usage Demo ===");
82 hazard_pointer_manager hp_manager(4, 2);
85 auto stats = hp_manager.get_statistics();
86 log_module::write_information(
"Initial statistics:");
87 log_module::write_information(
" Active hazard pointers: {}", stats.active_hazard_pointers);
88 log_module::write_information(
" Retired list size: {}", stats.retired_list_size);
89 log_module::write_information(
" Total retired: {}", stats.total_retired);
90 log_module::write_information(
" Total reclaimed: {}", stats.total_reclaimed);
93 std::atomic<TestNode*> test_ptr{
new TestNode(42)};
97 auto hp = hp_manager.acquire();
98 auto* protected_ptr = hp.protect(test_ptr);
100 log_module::write_information(
"Protected pointer value: {}", protected_ptr->data.load());
106 auto* node_to_retire = test_ptr.exchange(
nullptr);
107 hp_manager.retire(node_to_retire);
110 hp_manager.scan_and_reclaim();
113 stats = hp_manager.get_statistics();
114 log_module::write_information(
"Final statistics:");
115 log_module::write_information(
" Active hazard pointers: {}", stats.active_hazard_pointers);
116 log_module::write_information(
" Retired list size: {}", stats.retired_list_size);
117 log_module::write_information(
" Total retired: {}", stats.total_retired);
118 log_module::write_information(
" Total reclaimed: {}", stats.total_reclaimed);
122 log_module::write_information(
"\n=== Concurrent Access Demo ===");
124 constexpr int NUM_THREADS = 4;
125 constexpr int OPERATIONS_PER_THREAD = 1000;
127 hazard_pointer_manager hp_manager(NUM_THREADS, 2);
131 for (
int i = 0; i < 100; ++i) {
135 std::atomic<int> push_count{0};
136 std::atomic<int> pop_count{0};
137 std::atomic<int> failed_pops{0};
139 auto start_time = std::chrono::high_resolution_clock::now();
141 std::vector<std::thread> threads;
142 threads.reserve(NUM_THREADS);
145 for (
int thread_id = 0; thread_id < NUM_THREADS; ++thread_id) {
146 threads.emplace_back([&, thread_id]() {
147 std::random_device rd;
148 std::mt19937 gen(rd());
149 std::uniform_int_distribution<> dis(0, 1);
151 for (
int op = 0; op < OPERATIONS_PER_THREAD; ++op) {
154 int value = thread_id * OPERATIONS_PER_THREAD + op;
156 push_count.fetch_add(1);
161 pop_count.fetch_add(1);
163 failed_pops.fetch_add(1);
171 for (
auto& thread : threads) {
175 auto end_time = std::chrono::high_resolution_clock::now();
176 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
178 log_module::write_information(
"Concurrent operations completed in {} ms", duration.count());
179 log_module::write_information(
"Push operations: {}", push_count.load());
180 log_module::write_information(
"Successful pop operations: {}", pop_count.load());
181 log_module::write_information(
"Failed pop operations: {}", failed_pops.load());
184 auto stats = hp_manager.get_statistics();
185 log_module::write_information(
"Final hazard pointer statistics:");
186 log_module::write_information(
" Active hazard pointers: {}", stats.active_hazard_pointers);
187 log_module::write_information(
" Retired list size: {}", stats.retired_list_size);
188 log_module::write_information(
" Total retired: {}", stats.total_retired);
189 log_module::write_information(
" Total reclaimed: {}", stats.total_reclaimed);
193 log_module::write_information(
"\n=== Memory Safety Demo ===");
195 hazard_pointer_manager hp_manager(2, 1);
196 std::atomic<TestNode*> shared_ptr{
new TestNode(123)};
198 std::atomic<bool> reader_done{
false};
199 std::atomic<bool> writer_done{
false};
202 std::thread reader([&]() {
203 auto hp = hp_manager.acquire();
205 for (
int i = 0; i < 100; ++i) {
206 auto* protected_ptr = hp.protect(shared_ptr);
209 volatile int value = protected_ptr->data.load();
211 std::this_thread::sleep_for(std::chrono::microseconds(10));
214 std::this_thread::sleep_for(std::chrono::microseconds(10));
220 std::thread writer([&]() {
221 std::this_thread::sleep_for(std::chrono::milliseconds(10));
223 for (
int i = 0; i < 10; ++i) {
224 auto* new_node =
new TestNode(456 + i);
225 auto* old_node = shared_ptr.exchange(new_node);
228 hp_manager.retire(old_node);
231 std::this_thread::sleep_for(std::chrono::milliseconds(5));
240 auto* final_node = shared_ptr.exchange(
nullptr);
242 hp_manager.retire(final_node);
245 hp_manager.scan_and_reclaim();
247 auto stats = hp_manager.get_statistics();
248 log_module::write_information(
"Memory safety test completed safely!");
249 log_module::write_information(
"Final statistics:");
250 log_module::write_information(
" Total retired: {}", stats.total_retired);
251 log_module::write_information(
" Total reclaimed: {}", stats.total_reclaimed);