POSIX Shared Memory Data Structures 1.0
High-performance lock-free data structures for inter-process communication
Loading...
Searching...
No Matches
basic_usage.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <chrono>
3#include <thread>
4#include <cstring>
5#include "posix_shm.h"
6#include "shm_array.h"
7#include "shm_queue.h"
8
9int main() {
10 try {
11 // Create or open shared memory segment
12 constexpr size_t shm_size = 1024 * 1024; // 1MB
13 posix_shm shm("my_simulation_shm", shm_size);
14
15 std::cout << "Shared memory created/opened successfully\n";
16
17 // Example 1: Using shm_array for fixed-size data exchange
18 {
19 constexpr size_t array_size = 100;
20 shm_array<double> sensor_data(shm, "sensor_readings", array_size);
21
22 // Simulate writing sensor data
23 for (size_t i = 0; i < 10; ++i) {
24 sensor_data[i] = 3.14 * i;
25 }
26
27 std::cout << "Written sensor data: ";
28 for (size_t i = 0; i < 10; ++i) {
29 std::cout << sensor_data[i] << " ";
30 }
31 std::cout << "\n";
32 }
33
34 // Example 2: Discovery of existing array
35 {
36 // Another process could discover this array by name
37 shm_array<double> discovered_array(shm, "sensor_readings");
38 std::cout << "Discovered array size: " << discovered_array.size() << "\n";
39 std::cout << "First value: " << discovered_array[0] << "\n";
40 }
41
42 // Example 3: Using shm_queue for message passing
43 {
44 struct SimulationMessage {
45 uint64_t timestamp;
46 double value;
47 uint32_t sensor_id;
48 };
49
50 constexpr size_t queue_capacity = 50;
51 shm_queue<SimulationMessage> msg_queue(shm, "sim_messages", queue_capacity);
52
53 // Enqueue some messages
54 SimulationMessage msg1{1000, 42.5, 1};
55 SimulationMessage msg2{2000, 37.2, 2};
56
57 if (msg_queue.enqueue(msg1)) {
58 std::cout << "Message 1 enqueued successfully\n";
59 }
60 if (msg_queue.enqueue(msg2)) {
61 std::cout << "Message 2 enqueued successfully\n";
62 }
63
64 // Dequeue and display
65 SimulationMessage received;
66 if (msg_queue.dequeue(received)) {
67 std::cout << "Dequeued message - Timestamp: " << received.timestamp
68 << ", Value: " << received.value
69 << ", Sensor: " << received.sensor_id << "\n";
70 }
71
72 std::cout << "Queue size: " << msg_queue.size() << "\n";
73 }
74
75 // The shared memory will be automatically cleaned up when the last reference is released
76 std::cout << "\nShared memory operations completed successfully!\n";
77
78 } catch (const std::exception& e) {
79 std::cerr << "Error: " << e.what() << std::endl;
80 return 1;
81 }
82
83 return 0;
84}
int main()
Fixed-size array in shared memory with zero-overhead access.
Definition shm_array.h:63
size_t size() const noexcept
Get number of elements.
Definition shm_array.h:221
Lock-free circular queue for shared memory IPC.
Definition shm_queue.h:24
size_t size() const noexcept
Definition shm_queue.h:172
bool enqueue(const T &value) noexcept
Enqueue an element (lock-free)
Definition shm_queue.h:115
std::optional< T > dequeue() noexcept
Dequeue an element (lock-free)
Definition shm_queue.h:133
Core POSIX shared memory management with automatic reference counting.
Fixed-size shared memory array with STL compatibility.