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(), **);
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 == ){ //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", ); //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 = ; i < ; ++i) //Insert data in the vector
myvector->push_back(i); //Launch child process
std::string s(argv[]); s += " child ";
if( != std::system(s.c_str()))
return ; //Check child has destroyed the vector
if(segment.find<MyVector>("MyVector").first)
return ;
}
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 ;
};

boost::interprocess::managed_shared_memory(2)(std::deque)的更多相关文章

  1. boost::interprocess::managed_shared_memory(2)(std::string)

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

  2. boost::interprocess(1)

    发送端:#include <iostream> #include <windows.h> #include <string> using namespace std ...

  3. boost::interprocess::shared_memory_object(1)(基本类型)

    #include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> struct pos2d ...

  4. boost信号量 boost::interprocess::interprocess_semaphore的用法

    使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数. boost::interprocess::interprocess_semaphore m_s ...

  5. boost::interprocess(2)

    //doc_anonymous_mutex_shared_data.hpp #include <boost/interprocess/sync/interprocess_mutex.hpp> ...

  6. C++ std::deque

    std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...

  7. std::deque

    deque容器为一个给定类型的元素进行线性处理,像向量一样,它能够快速地随机访问任一个元素,并且能够高效地插入和删除容器的尾部元素.但它又与vector不同,deque支持高效插入和删除容器的头部元素 ...

  8. C++ std::deque 基本用法

    #include <iostream> #include <string> #include <deque> // https://zh.cppreference. ...

  9. Boost.Interprocess

    https://github.com/svebert/InterprocessMsg 好像消息队列

随机推荐

  1. Drupal启动过程

    Drupal整个启动过程共分为8个阶段: DRUPAL_BOOTSTRAP_CONFIGURATION:initialize configuration DRUPAL_BOOTSTRAP_PAGE_C ...

  2. redis可视化工具的安装和调试

    Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  3. Python-常用库扩展

    图片处理: PIL HTTP请求模拟: requests

  4. linux 常用的命令(转)

    常用指令 ls        显示文件或目录 -l           列出文件详细信息l(list) -a          列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir     ...

  5. unity, access sprite of UGUI Image

    首先需要using UnityEngine.UI; 然后调用下面语句就不报错了: Image.GetComponent<Image>().sprite 参考:http://answers. ...

  6. HTTP Basic auth认证

    Basic 概述 Basic 认证是HTTP 中非常简单的认证方式,因为简单,所以不是很安全,不过仍然非常常用. 当一个客户端向一个需要认证的HTTP服务器进行数据请求时,如果之前没有认证过,HTTP ...

  7. 创建一个很大的EMP表 EMP_LARGE

    --CREATE TABLE EMP_LARGE AS SELECT * FROM EMP ; ---先复制一张EMP表 DECLARE --声明变量 v_loop NUMBER; v_num NUM ...

  8. mysql唯一主键生成代码

    <insert id="insertPeople" parameterType="PeopleSchedule">     <selectKe ...

  9. IEnumerable扩展

    void Main() { // This uses a custom 'Pair' extension method, defined below. Customers .Select (c =&g ...

  10. 数据泵导出oracle 10g数据库

    首先连接sqlplus: sqlplus /nolog conn system/manager (或者连接其他用户) 1.创建whboa目录,用于存放导出的dmp文件(需要提前手动创建目录“E:\or ...