Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* @file demo_read_array.cpp |
3 |
|
|
* @author Vincent Berenz |
4 |
|
|
* license License BSD-3-Clause |
5 |
|
|
* @copyright Copyright (c) 2019, New York University and Max Planck |
6 |
|
|
* Gesellschaft. |
7 |
|
|
* @date 2019-05-22 |
8 |
|
|
* |
9 |
|
|
* @brief example of reading from interprocesses arrays |
10 |
|
|
* |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include <signal.h> |
14 |
|
|
#include <unistd.h> |
15 |
|
|
#include "shared_memory/array.hpp" |
16 |
|
|
#include "shared_memory/demos/item.hpp" |
17 |
|
|
|
18 |
|
|
#define SIZE 4 |
19 |
|
|
#define SEGMENT_SERIALIZED "demo_array_serialized" |
20 |
|
|
#define SEGMENT_FUNDAMENTAL "demo_array_fundamental" |
21 |
|
|
#define SEGMENT_FUNDAMENTAL_ARRAY "demo_array_fundamental_array" |
22 |
|
|
|
23 |
|
|
static bool RUNNING = true; |
24 |
|
|
|
25 |
|
✗ |
void stop(int) |
26 |
|
|
{ |
27 |
|
✗ |
RUNNING = false; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
✗ |
void print_array(int* values, int size) |
31 |
|
|
{ |
32 |
|
✗ |
for (int i = 0; i < size; i++) |
33 |
|
|
{ |
34 |
|
✗ |
std::cout << values[i] << " "; |
35 |
|
|
} |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
/** |
39 |
|
|
* @brief read from the array created by demo_write_array, which |
40 |
|
|
* should have been started before this demo was |
41 |
|
|
* (infinite hanging expected otherwise) |
42 |
|
|
*/ |
43 |
|
|
|
44 |
|
✗ |
void run() |
45 |
|
|
{ |
46 |
|
✗ |
bool mutex_protected = true; |
47 |
|
|
|
48 |
|
|
// because done in demo_write_array |
49 |
|
✗ |
bool clear_on_destruction = false; |
50 |
|
|
|
51 |
|
|
shared_memory::array<shared_memory::Item<SIZE> > serialized( |
52 |
|
✗ |
SEGMENT_SERIALIZED, SIZE, mutex_protected, clear_on_destruction); |
53 |
|
|
|
54 |
|
|
shared_memory::array<int> fundamental( |
55 |
|
✗ |
SEGMENT_FUNDAMENTAL, SIZE, mutex_protected, clear_on_destruction); |
56 |
|
|
|
57 |
|
|
shared_memory::array<int, SIZE> fundamental_array( |
58 |
|
✗ |
SEGMENT_FUNDAMENTAL_ARRAY, SIZE, mutex_protected, clear_on_destruction); |
59 |
|
|
|
60 |
|
✗ |
shared_memory::Item<SIZE> item; |
61 |
|
|
int values[SIZE]; |
62 |
|
|
int value; |
63 |
|
|
|
64 |
|
✗ |
while (RUNNING) |
65 |
|
|
{ |
66 |
|
✗ |
for (int i = 0; i < SIZE; i++) |
67 |
|
|
{ |
68 |
|
✗ |
serialized.get(i, item); |
69 |
|
✗ |
fundamental.get(i, value); |
70 |
|
✗ |
fundamental_array.get(i, values); |
71 |
|
✗ |
item.compact_print(); |
72 |
|
✗ |
std::cout << " | " << value << " | "; |
73 |
|
✗ |
print_array(values, SIZE); |
74 |
|
✗ |
std::cout << " | "; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
✗ |
std::cout << std::endl; |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
✗ |
int main() |
82 |
|
|
{ |
83 |
|
|
// stop on ctrl+c |
84 |
|
|
struct sigaction cleaning; |
85 |
|
✗ |
cleaning.sa_handler = stop; |
86 |
|
✗ |
sigemptyset(&cleaning.sa_mask); |
87 |
|
✗ |
cleaning.sa_flags = 0; |
88 |
|
✗ |
sigaction(SIGINT, &cleaning, nullptr); |
89 |
|
|
|
90 |
|
✗ |
run(); |
91 |
|
|
} |
92 |
|
|
|