-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_reader_writer_shm.cpp
40 lines (33 loc) · 1.06 KB
/
data_reader_writer_shm.cpp
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
#include "shared_data.h"
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <cstdio>
#include <iostream>
using namespace boost::interprocess;
int main()
{
// Remove shared memory on destruction
struct shm_remove
{
~shm_remove()
{
shared_memory_object::remove("MySharedMemory");
}
} remover;
// Open the shared memory object.
shared_memory_object shm(open_only, "MySharedMemory", read_write);
// Map the whole shared memory in this process
mapped_region region(shm, read_write);
// Get the address of the mapped region
void* addr = region.get_address();
// Construct the shared structure in memory
auto image = static_cast<Image*>(addr);
scoped_lock<interprocess_mutex> lock(image->mutex);
for (std::size_t i = 0; i < Image::size; ++i)
{
const auto current_pixel = image->data.at(i);
image->data.at(i) = current_pixel + 1;
}
return 0;
}