c++boost library learning-06-shared memory

Shared Memory
Shared memory is usually the fastest way of interprocess communication. It provides a memory area shared between processes. One process can write data to this area and another process can read it.

In boost.interprocess, the class boost::interprocess::shared_memory_object is used to represent shared memory. Include the header file boost/interprocess/shared_memory_object.hpp to use this class.

1 Create shared memory

Example. 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() << '\n';
  offset_t size;
  if (shdmem.get_size(size))
    std::cout << size << '\n';
}

boost::interprocess::shared_memory_object uses the constructor with three parameters.

The first parameter specifies whether shared memory should be created or simply opened. The example includes these two situations, that is to say boost::interprocess::open_or_create: open the shared memory if the shared memory already exists, and create the shared memory if the shared memory does not exist. Opening shared memory indicates that the shared memory was created before.
To uniquely identify shared memory, assign a name.

The second parameter specifies the name of boost::interprocess::shared_memory_object.

The third parameter specifies how the process accesses shared memory. In Example 33.1 boost::interprocess::read_write: indicates that the process has read and write access to the shared memory.

After creating an object of type boost::interprocess::shared_memory_object, the corresponding shared memory block will exist in the operating system. The size of this memory area is initially 0. To use this area, call truncate(), passing the parameter the size of the shared memory in bytes. In the example, shared memory provides 1,024 bytes of space. truncate() can only be called when the shared memory is opened in boost::interprocess::read_write mode. If not, an exception is thrown: boost::interprocess::interprocess_exception. truncate() can be called repeatedly to resize shared memory.

After creating shared memory, the member functions get_name() and get_size() can be used to query the name and size of the shared memory.

Since shared memory is used to exchange data between different processes, each process needs to map the shared memory into its address space. The class boost::interprocess::mapped_region is used to do this. That is, two are required to access shared memory (boost::interprocess::shared_memory_object and boost::interprocess::mapped_region). This is done so that the class boost::interprocess::mapped_region can also be used to map other objects into the process's address space.

2 Map shared memory into the address space of the process

Example. Mapping shared memory into the address space of a process

#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() << '\n';
  std::cout << std::dec << region.get_size() << '\n';
  mapped_region region2{
    
    shdmem, read_only};
  std::cout << std::hex << region2.get_address() << '\n';
  std::cout << std::dec << region2.get_size() << '\n';
}

To use class boost::interprocess::mapped_region, include the header file boost/interprocess/mapped_region.hpp.
The first parameter of the constructor of the boost::interprocess::mapped_region class is an object of the boost::interprocess::shared_memory_object class; the second parameter determines whether the access to the memory area is read only or read and write.

Example. Two objects of type boost::interprocess::mapped_region are created. A shared memory called Boost is mapped into the process's address space. The mapped_region member functions get_address() and get_size() obtain the address and size of the mapped memory area. get_size() returns 1024 in both cases, but get_address() returns different values.

3 Writing and reading from shared memory

Example. Writing and reading from shared memory

#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};
  int *i1 = static_cast<int*>(region.get_address());
  *i1 = 99;
  mapped_region region2{
    
    shdmem, read_only};
  int *i2 = static_cast<int*>(region2.get_address());
  std::cout << *i2 << '\n';
}

The example uses a mapped memory area to write and read a number. region writes the number 99 to the beginning of shared memory. Region2 then reads the same location in shared memory and writes the number to the standard output stream. Even though region and region2 represent different memory regions in the process, as shown by the get_address() return value in the previous example, the program prints 99 because both memory regions have access to the same underlying shared memory.

4 Delete shared memory

Example. Delete shared memory

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

using namespace boost::interprocess;

int main()
{
    
    
  bool removed = shared_memory_object::remove("Boost");
  std::cout << std::boolalpha << removed << '\n';
}

To delete shared memory, boost::interprocess::shared_memory_object provides 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 will continue to exist even if the program terminates. Whether shared memory is automatically deleted depends on the underlying operating system. Windows and many UNIX operating systems (including Linux) will automatically delete shared memory once the system is restarted.

Windows provides a special kind of shared memory that is automatically deleted once the last process using it terminates. You can access the boost::interprocess::windows_shared_memory class in boost/interprocess/windows_shared_memory.hpp to use this shared memory.

5 Using Windows-specific shared memory

Example. Using Windows-specific shared memory

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

using namespace boost::interprocess;

int main()
{
    
    
  windows_shared_memory shdmem{
    
    open_or_create, "Boost", read_write, 1024};
  mapped_region region{
    
    shdmem, read_write};
  int *i1 = static_cast<int*>(region.get_address());
  *i1 = 99;
  mapped_region region2{
    
    shdmem, read_only};
  int *i2 = static_cast<int*>(region2.get_address());
  std::cout << *i2 << '\n';
}

boost::interprocess::windows_shared_memory does not provide the member function truncate(). Instead, the size of the shared memory needs to be passed as the fourth parameter to the constructor of this class.

Class boost::interprocess::windows_shared_memory can only be used on Windows and is not portable.

Guess you like

Origin blog.csdn.net/FairLikeSnow/article/details/130796225