28 std::cout <<
"\n=== Particle Simulation with Object Pool ===\n";
30 posix_shm shm(
"particle_sim", 100 * 1024 * 1024);
33 constexpr size_t MAX_PARTICLES = 10000;
40 std::cout <<
"Pool capacity: " << particles.
capacity() <<
" particles\n";
43 std::vector<uint32_t> handles;
44 for (
int i = 0; i < 100; ++i) {
47 auto& p = particles[*h];
52 handles.push_back(*h);
57 std::cout <<
"Spawned " << handles.size() <<
" particles\n";
59 <<
"/" << particles.
capacity() <<
"\n";
62 for (
auto h : handles) {
63 auto& p = particles[h];
70 for (
size_t i = 0; i < handles.size() / 2; ++i) {
75 std::cout <<
"After destroying half:\n";
77 <<
"/" << particles.
capacity() <<
"\n";
78 std::cout <<
"Total spawned: " << total_spawned.
load() <<
"\n";
79 std::cout <<
"Total destroyed: " << total_destroyed.
load() <<
"\n";
83 std::cout <<
"\n=== Sensor Data Streaming with Ring Buffer ===\n";
85 posix_shm shm(
"sensor_stream", 10 * 1024 * 1024);
88 constexpr size_t BUFFER_SIZE = 1000;
92 std::random_device rd;
93 std::mt19937 gen(rd());
94 std::uniform_real_distribution<> temp_dist(20.0, 30.0);
95 std::uniform_real_distribution<> pressure_dist(1000.0, 1020.0);
97 auto now = std::chrono::system_clock::now();
100 for (
int i = 0; i < 50; ++i) {
102 .
timestamp = std::chrono::duration_cast<std::chrono::microseconds>(
103 now.time_since_epoch()).count() + i * 1000,
104 .temperature =
static_cast<float>(temp_dist(gen)),
105 .pressure =
static_cast<float>(pressure_dist(gen)),
106 .humidity = 45.0f + i * 0.1f
109 if (!sensor_buffer.
push(reading)) {
110 std::cout <<
"Buffer full at " << i <<
" readings\n";
115 std::cout <<
"Buffer contains " << sensor_buffer.
size() <<
" readings\n";
116 std::cout <<
"Total written: " << sensor_buffer.
total_written() <<
"\n";
120 size_t got = sensor_buffer.
get_last_n(5, last_readings);
121 std::cout <<
"\nLast " << got <<
" readings:\n";
122 for (
size_t i = 0; i < got; ++i) {
124 <<
"°C, P=" << last_readings[i].
pressure <<
" hPa\n";
129 size_t read = sensor_buffer.
pop_bulk(batch);
130 std::cout <<
"\nProcessed " << read <<
" readings in batch\n";
131 std::cout <<
"Buffer now contains " << sensor_buffer.
size() <<
" readings\n";
135 std::cout <<
"\n=== Grid-based Simulation ===\n";
137 posix_shm shm(
"grid_sim", 50 * 1024 * 1024);
140 constexpr size_t GRID_SIZE = 256 * 256;
158 std::cout <<
"Grid size: " << 256 <<
"x" << 256
159 <<
" = " << GRID_SIZE <<
" cells\n";
160 std::cout <<
"Memory per grid: " <<
sizeof(Cell) * GRID_SIZE / 1024
164 for (
size_t i = 0; i < 100; ++i) {
165 grid_current[i].density = 1.0f;
166 grid_current[i].type = 1;
170 for (
size_t y = 1; y < 255; ++y) {
171 for (
size_t x = 1; x < 255; ++x) {
172 size_t idx = y * 256 + x;
175 float avg_density = (
176 grid_current[idx - 256].density +
177 grid_current[idx + 256].density +
178 grid_current[idx - 1].density +
179 grid_current[idx + 1].density
182 grid_next[idx].density = avg_density;
187 std::cout <<
"Simulation at step: " << step_counter.
load() <<
"\n";
191 std::cout <<
"\n=== Common Simulation Patterns ===\n\n";
193 std::cout <<
"1. Object Pool (shm_object_pool):\n";
194 std::cout <<
" - Dynamic entity management (particles, enemies, projectiles)\n";
195 std::cout <<
" - Avoids fragmentation, O(1) allocation\n";
196 std::cout <<
" - Perfect for systems with many temporary objects\n\n";
198 std::cout <<
"2. Ring Buffer (shm_ring_buffer):\n";
199 std::cout <<
" - Sensor data streams\n";
200 std::cout <<
" - Event/message logging\n";
201 std::cout <<
" - Time-series data with bulk operations\n\n";
203 std::cout <<
"3. Fixed Arrays (shm_array):\n";
204 std::cout <<
" - Spatial grids (collision, physics, fluid)\n";
205 std::cout <<
" - Lookup tables\n";
206 std::cout <<
" - Double-buffered state\n\n";
208 std::cout <<
"4. Atomics (shm_atomic):\n";
209 std::cout <<
" - Global counters and statistics\n";
210 std::cout <<
" - Synchronization flags\n";
211 std::cout <<
" - Lock-free coordination\n\n";
213 std::cout <<
"5. Queues (shm_queue):\n";
214 std::cout <<
" - Task distribution\n";
215 std::cout <<
" - Event handling\n";
216 std::cout <<
" - Producer-consumer patterns\n";
226 std::cout <<
"\n=== All simulation patterns demonstrated successfully! ===\n";
227 }
catch (
const std::exception& e) {
228 std::cerr <<
"Error: " << e.what() << std::endl;
Fixed-size array in shared memory with zero-overhead access.
Shared memory atomic value with auto-discovery.
T load(std::memory_order order=std::memory_order_seq_cst) const noexcept
High-performance object pool for shared memory.
void release(handle_type handle) noexcept
Release an object back to the pool.
size_t capacity() const noexcept
Get pool statistics.
std::optional< handle_type > acquire_construct(Args &&... args)
Acquire and construct an object.
size_t num_allocated() const noexcept
Lock-free ring buffer for high-throughput streaming data.
size_t pop_bulk(std::span< T > values) noexcept
Pop multiple elements efficiently.
bool push(const T &value) noexcept
Push a single element.
size_t get_last_n(size_t n, std::span< T > values) const noexcept
Get the last N elements (most recent) Useful for getting trailing sensor data.
size_t size() const noexcept
uint64_t total_written() const noexcept
Get total elements written (useful for monitoring data rate)
Core POSIX shared memory management with automatic reference counting.
Fixed-size shared memory array with STL compatibility.
void particle_simulation_example()
void grid_simulation_example()
void sensor_streaming_example()