Boost预报——通用几何库(GGL)

编程入门 行业动态 更新时间:2024-10-28 09:26:07

Boost预报——通用<a href=https://www.elefans.com/category/jswz/34/1769510.html style=几何库(GGL)"/>

Boost预报——通用几何库(GGL)

用共享内存作为匿名内存池

只要分配共享内存段的一部分作为缓存区,把信息复制进去,再把缓存区在共享内存中的偏移值发送给另外的进程,任务就完成啦!让我们看一个例子:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> //std::system
#include <sstream>int main (int argc, char *argv[])
{using namespace boost::interprocess;if(argc == 1){  //Parent process
      //Remove shared memory on construction and destruction
      struct shm_remove {shm_remove() {  shared_memory_object::remove("MySharedMemory"); }~shm_remove(){  shared_memory_object::remove("MySharedMemory"); }} remover;//Create a managed shared memory segment
      managed_shared_memory segment(create_only, "MySharedMemory", 65536);//Allocate a portion of the segment (raw memory)
      std::size_t free_memory = segment.get_free_memory();void * shptr = segment.allocate(1024/*bytes to allocate*/);//Check invariant
      if(free_memory <= segment.get_free_memory())return 1;//An handle from the base address can identify any byte of the shared 
      //memory segment even if it is mapped in different base addresses
      managed_shared_memory::handle_t handle = segment.get_handle_from_address(shptr);std::stringstream s;s << argv[0] << " " << handle;s << std::ends;//Launch child process
      if(0 != std::system(s.str().c_str()))return 1;//Check memory has been freed
      if(free_memory != segment.get_free_memory())return 1;}else{//Open managed segment
      managed_shared_memory segment(open_only, "MySharedMemory");//An handle from the base address can identify any byte of the shared 
      //memory segment even if it is mapped in different base addresses
      managed_shared_memory::handle_t handle = 0;//Obtain handle value
      std::stringstream s; s << argv[1]; s >> handle;//Get buffer local address from handle
      void *msg = segment.get_address_from_handle(handle);//Deallocate previously allocated memory
      segment.

更多推荐

Boost预报——通用几何库(GGL)

本文发布于:2024-03-05 15:26:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1712673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几何   Boost   GGL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!