-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_reader.cpp
More file actions
44 lines (32 loc) · 1.42 KB
/
basic_reader.cpp
File metadata and controls
44 lines (32 loc) · 1.42 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
#include <slick/shm/shared_memory.hpp>
#include <iostream>
#include <cstring>
int main() {
using namespace slick::shm;
const char* shm_name = "slick_shm_example";
try {
// Open existing shared memory
std::cout << "Opening existing shared memory '" << shm_name << "'..." << std::endl;
shared_memory shm(shm_name, open_existing, access_mode::read_only);
std::cout << "Shared memory opened successfully!" << std::endl;
std::cout << " Name: " << shm.name() << std::endl;
std::cout << " Size: " << shm.size() << " bytes" << std::endl;
std::cout << " Address: " << shm.data() << std::endl;
// Read the data
const char* data = static_cast<const char*>(shm.data());
std::cout << "\nMessage from shared memory: \"" << data << "\"" << std::endl;
std::cout << "\nPress Enter to exit..." << std::endl;
std::cin.get();
} catch (const shared_memory_error& e) {
std::cerr << "Error opening shared memory '" << shm_name << "': "
<< e.what() << std::endl;
std::cerr << "Error code: " << e.code() << " ("
<< e.code().message() << ")" << std::endl;
if (e.code() == errc::not_found) {
std::cerr << "\nShared memory '" << shm_name
<< "' not found. Make sure 'basic_writer' is running first." << std::endl;
}
return 1;
}
return 0;
}