zoukankan      html  css  js  c++  java
  • boost::interprocess::managed_shared_memory(2)(std::deque)

    struct shareDataEx : shareData
    {
        int index;
        int total_size;
    };
    typedef managed_shared_memory::segment_manager        segment_manager_t;  //段管理器
    typedef allocator<shareDataEx, segment_manager_t>   mem_allocator;  //定义基于shareDataEx类型的分配器
    typedef deque<shareDataEx, mem_allocator>           mem_queue;  //创建deque基于boost::Interprocess::containers::deque,使用mem_allocator分配器
    m_segment = new managed_shared_memory(open_or_create, getMemName(m_name + "ProcessMemPool9", pid).c_str(), 1024*1024*10);
    m_queue = m_segment->find_or_construct<mem_queue>(getMemName(m_name + "m_queue", pid).c_str())(mem_allocator(m_segment->get_segment_manager()));  
    mem_allocator(m_segment->get_segment_manager()好像是分配deque兼容的分配器,不是太懂,有时间看看下面这个例子:
    #include <boost/interprocess/managed_shared_memory.hpp>
    #include <boost/interprocess/containers/vector.hpp>
    #include <boost/interprocess/allocators/allocator.hpp>
    #include <string>
    #include <cstdlib> //std::system
    
    using namespace boost::interprocess;
    
    //Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
    //This allocator will allow placing containers in the segment
    typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;
    
    //Alias a vector that uses the previous STL-like allocator so that allocates
    //its values from the segment
    typedef vector<int, ShmemAllocator> MyVector;
    
    //Main function. For parent process argc == 1, for child process argc == 2
    int main(int argc, char *argv[])
    {
        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 new segment with given name and size
            managed_shared_memory segment(create_only, "MySharedMemory", 65536);
    
            //Initialize shared memory STL-compatible allocator
            const ShmemAllocator alloc_inst (segment.get_segment_manager());
    
            //Construct a vector named "MyVector" in shared memory with argument alloc_inst
            MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
    
            for(int i = 0; i < 100; ++i)  //Insert data in the vector
                myvector->push_back(i);
    
            //Launch child process
            std::string s(argv[0]); s += " child ";
            if(0 != std::system(s.c_str()))
                return 1;
    
            //Check child has destroyed the vector
            if(segment.find<MyVector>("MyVector").first)
                return 1;
        }
        else{ //Child process
            //Open the managed segment
            managed_shared_memory segment(open_only, "MySharedMemory");
    
            //Find the vector using the c-string name
            MyVector *myvector = segment.find<MyVector>("MyVector").first;
    
            //Use vector in reverse order
            std::sort(myvector->rbegin(), myvector->rend());
    
            //When done, destroy the vector from the segment
            segment.destroy<MyVector>("MyVector");
        }
        getchar();
        return 0;
    };
  • 相关阅读:
    Spring MVC学习03页面跳转
    Spring Boot学习07配置加载顺序
    Spring MVC学习01从空白Maven项目搭建环境
    Spring MVC学习05JSON序列化
    剑指Offer 44 数字序列中某一位的数字
    Spring MVC学习06异常解析器
    MSSQL·查看DB中所有表及列的相关信息
    MSSQL·查询数据库中所有索引的相关信息
    MSSQL·最长输出长度限制之解决方案
    .Net Core·热加载的实现及测试
  • 原文地址:https://www.cnblogs.com/zzyoucan/p/3756272.html
Copyright © 2011-2022 走看看