-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_sync.cpp
More file actions
110 lines (81 loc) · 3.48 KB
/
advanced_sync.cpp
File metadata and controls
110 lines (81 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <slick/shm/shared_memory_view.hpp>
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
#include <cstring>
#include <cstdio>
// Shared data structure
struct shared_data {
std::atomic<int> counter;
std::atomic<bool> done;
char message[256];
};
void writer_thread(slick::shm::shared_memory_view view) {
auto* data = static_cast<shared_data*>(view.data());
std::cout << "[Writer] Starting..." << std::endl;
for (int i = 0; i < 10; ++i) {
data->counter.store(i, std::memory_order_release);
std::string msg = "Message " + std::to_string(i);
std::snprintf(data->message, sizeof(data->message), "%s", msg.c_str());
std::cout << "[Writer] Wrote: " << msg << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
data->done.store(true, std::memory_order_release);
std::cout << "[Writer] Done." << std::endl;
}
void reader_thread(slick::shm::shared_memory_view view) {
auto* data = static_cast<shared_data*>(view.data());
std::cout << "[Reader] Starting..." << std::endl;
int last_count = -1;
while (!data->done.load(std::memory_order_acquire)) {
int current = data->counter.load(std::memory_order_acquire);
if (current != last_count) {
std::cout << "[Reader] Read: counter=" << current
<< ", message=\"" << data->message << "\"" << std::endl;
last_count = current;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << "[Reader] Done." << std::endl;
}
int main() {
using namespace slick::shm;
const char* shm_name = "slick_shm_advanced";
const std::size_t shm_size = sizeof(shared_data);
try {
// Clean up any existing shared memory
shared_memory::remove(shm_name);
// Create shared memory
std::cout << "Creating shared memory for advanced synchronization example..." << std::endl;
shared_memory shm(shm_name, shm_size, create_only);
// Initialize the shared data
// Note: Do NOT use placement new on std::atomic in shared memory across processes.
// std::atomic's constructor may not be safe for cross-process initialization.
// Instead, use explicit store operations on already-constructed atomics.
auto* data = static_cast<shared_data*>(shm.data());
// Initialize atomics with release semantics to establish happens-before relationship
data->counter.store(0, std::memory_order_release);
data->done.store(false, std::memory_order_release);
std::memset(data->message, 0, sizeof(data->message));
std::cout << "Shared memory created and initialized." << std::endl;
std::cout << "Starting writer and reader threads..." << std::endl;
// Create a view for thread safety
shared_memory_view view(shm);
// Start threads
std::thread writer(writer_thread, view);
std::thread reader(reader_thread, view);
// Wait for threads to complete
writer.join();
reader.join();
std::cout << "\nExample completed successfully!" << std::endl;
// Cleanup: Close and unmap before removing to avoid race with destructor
shm.close();
shared_memory::remove(shm_name);
} catch (const shared_memory_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Error code: " << e.code().value() << std::endl;
return 1;
}
return 0;
}