Monday, December 21, 2009

Boost Shared Memory Vector Reader

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Boost Shared Memory Reader
// This vector is read from a shared memory
// Date : 26 - July -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

#include <iterator>
#include <functional>
#include <utility>
#include <sys/time.h>
#include <time.h>
#include <iostream>

#define NUM_ENTRIES 10

int main ()
{
using namespace boost::interprocess;

typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
typedef vector<int, ShmemAllocator> MyVector;

try{
//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;

//size of vector
std::cout << "size = " << myvector->size() << std::endl;


MyVector::iterator iter = myvector->begin();
while(iter++ != myvector->end())
std::cout << "nvalue = " << *iter ;


//When done, destroy the vector from the segment
//segment.destroy<MyVector>("MyVector");
}
catch(...){
std::cout<<"n Exception caught";
throw;
}
return 0;
}

No comments:

Post a Comment