码迷,mamicode.com
首页 > 其他好文 > 详细

boost Shared Memory

时间:2019-07-31 12:43:37      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:har   change   window   eth   including   ted   depends   boost   output   

Shared memory is typically the fastest form of interprocess communicatioin. It provides a memory area that is shared between processes. One process can write data to the area and another process can read it.

In Boost.Interrprocess the class boost::interprocess::shared_memory_object is used to represent shared memory.

1. create shared memory

#include <boost/interprocess/shared_memory_object.hpp>
#include <iostream>

using namespace boost::interprocess;

int main() {
  shared_memory_object shdmem(open_or_create, "Boost", read_write);
  shdmem.truncate(1024);
  std::cout << shdmem.get_name() << std::endl;
  offset_t size;
  if (shdmem.get_size(size)) {
    std::cout << size << std::endl;
  }

  return 0;
}

shared_memory_object expects three parameters. The first parameter specifies whether the shared memory should be created or just opened. boost::interprocess::open_or_create will open shared memory if it already exists or create shared memory if it doesn‘t.

The name is specified by the second parameter passed to the constructor of boost::interprocess::shared_memeory_object.

The third parameter determines how a process can access shared memory. boost::interprocess::read_write says the process has read-write access.

After creating an object of type boost::interprocess::shared_memory_object, a corresponding shared memory block will exist within the operating system. The size of this memory area is initially 0. To use the area, call truncate(), passing in the size of the shared memory in bytes.

get_name() and get_size() can be used to query the name and the size of the shared memory.

 

2. Because shared memory is used to exchange data betwen different processes, each process needs to map the shared memory into its address space.

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>

using namespace boost::interprocess;

int main() {
  shared_memory_object shdmem(open_or_create, "Boost", read_write);
  shdmem.truncate(1024);
  mapped_region region(shdmem, read_write);
  std::cout << std::hex << region.get_address() << std::endl;
  std::cout << std::dec << region.get_size() << std::endl;
  int* i1 = static_cast<int*>(region.get_address());
  *i1 = 99;

  mapped_region region2(shdmem, read_only);
  std::cout << std::hex << region2.get_address() << std::endl;
  std::cout << std::dec << region2.get_size() << std::endl;
  int* i2 = static_cast<int*>(region2.get_address());
  std::cout << *i2 << std::endl;

  bool removed = shared_memory_object::remove("Boost");
  std::cout << std::boolalpha << removed << std::endl;
  return 0;
}

输出为:

0x7f6abbf76000

1024

0x7f6abbf74000

1024

99

true

boost::interprocess::shared_memory_object must be passed as the first parameter to the constructor of boost::interprocess::mapped_region. The second parameter determines whether access to the memory area is read-only or read-write. The address and the size of the mapped memory area is written to standard output using the member funcitons get_address() and get_size(). The return value of get_address() is different for each object.

As the example above, region writes the number 99 to the beginning of the shared memory. region2 the reads the same location in shared memory and writes the number to the standard ouptu stream. Even though region and region2 represent different memory areas within the process, the program print2 99 because both memory areas access the same underlying shared memory.

To delete shared memory, boost::interprocess::shared_memory_object offers the static member function remove(), which takes the name of the shared memory to be deleted as a parameter.

If remove() is never called, the shared memory continues to exist even if the program terminates. Whether or not the shared memory is automatically deleted depends on the underlying operating system. Windows and many Unix operating systems, including Linux, automatically delete shared memory once the system is restarted.

boost Shared Memory

标签:har   change   window   eth   including   ted   depends   boost   output   

原文地址:https://www.cnblogs.com/sssblog/p/11275429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!