只要分配共享内存段的一部分作为缓存区,把信息复制进去,再把缓存区在共享内存中的偏移值发送给另外的进程,任务就完成啦!让我们看一个例子:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> #include <sstream>
int main (int argc, char *argv[])
{
using namespace boost::interprocess;
if(argc == 1){ struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
std::size_t free_memory = segment.get_free_memory();
void * shptr = segment.allocate(1024);
if(free_memory <= segment.get_free_memory())
return 1;
managed_shared_memory::handle_t handle = segment.get_handle_from_address(shptr);
std::stringstream s;
s << argv[0] << " " << handle;
s << std::ends;
if(0 != std::system(s.str().c_str()))
return 1;
if(free_memory != segment.get_free_memory())
return 1;
}
else{
managed_shared_memory segment(open_only, "MySharedMemory");
managed_shared_memory::handle_t handle = 0;
std::stringstream s; s << argv[1]; s >> handle;
void *msg = segment.get_address_from_handle(handle);
segment.deallocate(msg);
}
return 0;
}
你可以在共享内存段中建立对象,赋于这个对象一个字符串名称,这样其它进程就可以找到、使用它们,以及当不再需要时删除它们。例:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> #include <cstddef>
#include <cassert>
#include <utility>
int main(int argc, char *argv[])
{
using namespace boost::interprocess;
typedef std::pair<double, int> MyType;
if(argc == 1){ struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
MyType *instance = segment.construct<MyType>
("MyType instance") (0.0, 0);
MyType *array = segment.construct<MyType>
("MyType array") [10] (0.0, 0);
float float_initializer[3] = { 0.0, 1.0, 2.0 };
int int_initializer[3] = { 0, 1, 2 };
MyType *array_it = segment.construct_it<MyType>
("MyType array from it") [3] ( &float_initializer[0] , &int_initializer[0]);
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;
if(segment.find<MyType>("MyType array").first ||
segment.find<MyType>("MyType instance").first ||
segment.find<MyType>("MyType array from it").first)
return 1;
}
else{
managed_shared_memory segment(open_only, "MySharedMemory");
std::pair<MyType*, std::size_t> res;
res = segment.find<MyType> ("MyType array");
if(res.second != 10) return 1;
res = segment.find<MyType> ("MyType instance");
if(res.second != 1) return 1;
res = segment.find<MyType> ("MyType array from it");
if(res.second != 3) return 1;
segment.destroy<MyType>("MyType array");
segment.destroy<MyType>("MyType instance");
segment.destroy<MyType>("MyType array from it");
}
return 0;
}
Boost.Interprocess 允许在共享内存和内存映射文件中建立复杂对象。例如,我们可以在共享内存中构造类STL的容器。实现它我们只需要建立一个特定的(受控的 managed)共享内存段,声明一个Boost.Interprocess分配器,并且如同其它对象一样,在共享内存中构造vector。
允许在共享内存中应用复杂结构的类称为 boost::interprocess::managed_shared_memory
,它很容易使用。不带参数地运行下面的例子:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <cstdlib>
using namespace boost::interprocess;
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
typedef vector<int, ShmemAllocator> MyVector;
int main(int argc, char *argv[])
{
if(argc == 1){ struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
const ShmemAllocator alloc_inst (segment.get_segment_manager());
MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
for(int i = 0; i < 100; ++i) myvector->push_back(i);
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;
if(segment.find<MyVector>("MyVector").first)
return 1;
}
else{ managed_shared_memory segment(open_only, "MySharedMemory");
MyVector *myvector = segment.find<MyVector>("MyVector").first;
std::sort(myvector->rbegin(), myvector->rend());
segment.destroy<MyVector>("MyVector");
}
return 0;
};
父进程建立一个特殊共享内存类,允许简单地构造多个与名称关联的复杂结构。之后父进程带参数地启动同一程序,子进程打开共享内存并使用vector和删除它。
就象vector一样,Boost.Interprocess可以在共享内存和内存映射文件中建立map。唯一的区别是,和标准关联容器一样,Boost.Interprocess的map在构造方法中传入分配器的同时还需要比较算子:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <utility>
int main ()
{
using namespace boost::interprocess;
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment
(create_only
,"MySharedMemory" ,65536);
typedef int KeyType;
typedef float MappedType;
typedef std::pair<const int, float> ValueType;
typedef allocator<ValueType, managed_shared_memory::segment_manager>
ShmemAllocator;
typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap;
ShmemAllocator alloc_inst (segment.get_segment_manager());
MyMap *mymap =
segment.construct<MyMap>("MyMap") (std::less<int>() ,alloc_inst);
for(int i = 0; i < 100; ++i){
mymap->insert(std::pair<const int, float>(i, (float)i));
}
return 0;
}
更多高级例子,包括容器的容器,请参考容器的容器一节。