2010-10-27 38 views
1

我想了解boost进程库如何工作。 我有一个设备类,它保存向量容器中的整数值。使用boost进程访问共享对象的向量元素

在父母进程中; 我构建的设备对象MySegmentObject段,并在此对象的构造函数中创建了MySegmentVector段的向量。

使用子进程; 我想访问创建的对象并获取矢量的大小。我可以使用segment->find方法访问对象,但是当我从访问的对象中调用getSize()方法时,它会崩溃!

我在做什么错,可能我错过了关于共享内存的概念。

我测试的代码上的Visual Studio 2010 &升压1.43.0 LIB

Equipments.h

#pragma once 
#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/containers/vector.hpp> 
#include <boost/interprocess/allocators/allocator.hpp> 

using namespace boost::interprocess; 

typedef boost::interprocess::allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator; 
typedef boost::container::vector<int, ShmemAllocator> EqVector; 

class Equipments { 
public:  
    Equipments(void); 
    ~Equipments(void); 
    void addEquipment(int n); 
    int getSize(); 
    int getElement(int n); 

private: 
    const ShmemAllocator *alloc_inst; 
    <offset_ptr>EqVector eqVector; 
    managed_shared_memory *segment; 
}; 

Equipments.cpp

#include "StdAfx.h" 
#include "Equipments.h" 
#include <iostream> 

Equipments::Equipments(void) 
{ 
    shared_memory_object::remove("mySegmentVector"); 
    segment = new managed_shared_memory(create_only, "mySegmentObjectVector", 65536); 
    alloc_inst = new ShmemAllocator(segment->get_segment_manager()); 
    eqVector = segment->construct<EqVector>("myVector")(*alloc_inst); 
} 


Equipments::~Equipments(void) 
{ 
} 

void Equipments::addEquipment(int n) 
{ 
    eqVector->push_back(n); 
} 

int Equipments::getSize() 
{ 
    return eqVector->size(); 
} 

int Equipments::getElement(int n) 
{ 
    return eqVector->at(n); 
} 

Main.cpp的

#include "stdafx.h" 
#include "Equipments.h" 
#include <iostream> 
#include <string> 
#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/containers/vector.hpp> 
#include <boost/interprocess/allocators/allocator.hpp> 

using namespace boost::interprocess; 

int main(int argc, char *argv[]) 
{ 
    if (argc == 1) 
    { 
     std::cout << "parent process" << std::endl; 
     shared_memory_object::remove("mySegmentObject"); 
     managed_shared_memory segment(create_only, "mySegmentObject", 65536); 
     Equipments *eqPtr = segment.construct<Equipments>("EQ")(); 

     eqPtr->addEquipment(19); 
     eqPtr->addEquipment(12); 

     //Launch child process 
     std::string s(argv[0]); s += " child "; 
     if(0 != std::system(s.c_str())) 
      return 1;  //Launch child process 
    } 
    else 
    { 
     std::cout << "child process" << std::endl; 
     managed_shared_memory *segment = new managed_shared_memory(open_only, "mySegmentObject"); 
     std::pair<Equipments*, std::size_t> p = segment->find<Equipments>("EQ"); 

     if (p.first) 
     { 
      std::cout << "EQ found" << std::endl;  
     std::cout << p.first->getSize() << std::endl;  
     } 
     else 
     { 
      std::cout << "EQ not found" << std::endl; 
     } 
    } 
} 
+0

EqVector eqVector已更改EqVector * eqVector EqVector eqVector并使用eqVector-> push_back(n)矢量,但没有我看到相同的错误.. – penguru 2010-10-28 11:30:07

回答

5

问题是你正在使用一个普通的指针来存储你的EqVector。在映射共享内存段时,它可能映射到进程地址空间中的任何位置。这意味着,eqVector存储在内存中的位置在第一个进程的内存空间中与第二个进程中的不同。您需要使用boost :: offset_ptr,它将地址存储为共享内存段开始时的偏移量,请参阅:http://www.boost.org/doc/libs/1_35_0/doc/html/interprocess/offset_ptr.html

+0

+1超出了责任的使命,受制于此工作。 – 2010-10-27 23:25:20

+0

其实我也可以通过查找方法访问向量,我想通过指针访问。 – penguru 2010-10-28 08:13:52