boost Shared Memory
Posted c++life
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了boost Shared Memory相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
boost::interprocess::shared_memory_object::remove 失败
如何从 boost::interprocess::managed_shared_memory 对象中获取 shmid
在 Boost Interprocess managed_shared_memory 中搜索时,线程陷入互斥等待
boost::interprocess::managed_shared_memory() open_only 在 XP 上失败