
Hi, Is something like this likely to work? typedef interprocess::vector<int, ShmemAllocator> MyVector typedef std::pair<const long, MyVector> ValueType; typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator; typedef interprocess::map<long, MyVector, std::less<long>, ShmemAllocator> MyMap;

Jan Stetka wrote:
Hi,
Is something like this likely to work?
typedef interprocess::vector<int, ShmemAllocator> MyVector
typedef std::pair<const long, MyVector> ValueType;
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
typedef interprocess::map<long, MyVector, std::less<long>, ShmemAllocator> MyMap;
Yes, it should: #include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/containers/map.hpp> #include <boost/interprocess/managed_shared_memory.hpp> using namespace boost::interprocess; typedef managed_shared_memory::segment_manager Mngr; typedef allocator<int, Mngr> ShmemIntAllocator; typedef vector<int, ShmemIntAllocator> MyVector; typedef std::pair<const long, MyVector>ValueType; typedef allocator<ValueType, Mngr> ShmemValueTypeAllocator; typedef map<long, MyVector , std::less<long>, ShmemValueTypeAllocator> MyMap; int main() { { shared_memory_object::remove("name"); managed_shared_memory shmem(open_or_create, "name", 65536); MyMap *m = shmem.construct<MyMap>("Map name") (std::less<long>(), shmem.get_segment_manager()); //This is the usual way ValueType v(0, MyVector(shmem.get_segment_manager())); m->insert(v); //This is more efficient(data is moved instead of copied) //Note that "long" is not "const long" std::pair<long, MyVector> mv (1, MyVector(shmem.get_segment_manager())); m->insert(move(mv)); //Even more efficient, in-place construction: //the first argument is for the key (long), //the rest are for the mapped type m->emplace(2, shmem.get_segment_manager()); } shared_memory_object::remove("name"); return 0; }

phshCPUTime; typedef boost::shmem::vector<pair<unsigned long,ptime> ,boost::shmem::allocator<pair<unsigned long,ptime>,boost::shmem::named_shared_object::segment_manager > >
When I try to compile: typedef boost::shmem::vector<pair<float,ptime> ,boost::shmem::allocator<pair<float,ptime>,boost::shmem::named_shared_object::segment_manager phshMemory; struct PHSharedData { unsigned long TPM; phshCPUTime CPUTime; phshMemory Memory; }; PHSharedData phsd; phsd.CPUTime =phshCPUTime(_segment.get_segment_manager()); results in: d:\boost_1_35_0\boost\shmem\containers\vector.hpp(274) : error C2512: 'boost::shmem::allocator<T,SegmentManager>::__ctor' : no appropriate default constructor available with [ T=std::pair<float,boost::posix_time::ptime>, SegmentManager=boost::shmem::detail::basic_named_object_impl<char,boost::shmem::simple_seq_fit<boost::shmem::shared_mutex_family>,boost::shmem::flat_map_index>::segment_manager which i plan to use in this map: typedef boost::shmem::map<long,PHSharedData,less<long>,boost::shmem::allocator<pair<long,PHSharedData>, boost::shmem::named_shared_object::segment_manager> > phshpd; Ion Gaztañaga wrote:
Jan Stetka wrote:
Hi,
Is something like this likely to work?
typedef interprocess::vector<int, ShmemAllocator> MyVector
typedef std::pair<const long, MyVector> ValueType;
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
typedef interprocess::map<long, MyVector, std::less<long>, ShmemAllocator> MyMap;
Yes, it should:
#include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/containers/map.hpp> #include <boost/interprocess/managed_shared_memory.hpp>
using namespace boost::interprocess;
typedef managed_shared_memory::segment_manager Mngr;
typedef allocator<int, Mngr> ShmemIntAllocator; typedef vector<int, ShmemIntAllocator> MyVector; typedef std::pair<const long, MyVector>ValueType; typedef allocator<ValueType, Mngr> ShmemValueTypeAllocator; typedef map<long, MyVector , std::less<long>, ShmemValueTypeAllocator> MyMap;
int main() { { shared_memory_object::remove("name"); managed_shared_memory shmem(open_or_create, "name", 65536);
MyMap *m = shmem.construct<MyMap>("Map name") (std::less<long>(), shmem.get_segment_manager());
//This is the usual way ValueType v(0, MyVector(shmem.get_segment_manager())); m->insert(v); //This is more efficient(data is moved instead of copied) //Note that "long" is not "const long" std::pair<long, MyVector> mv (1, MyVector(shmem.get_segment_manager())); m->insert(move(mv));
//Even more efficient, in-place construction: //the first argument is for the key (long), //the rest are for the mapped type m->emplace(2, shmem.get_segment_manager()); } shared_memory_object::remove("name"); return 0; } _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Jan Stetka wrote:
When I try to compile:
phshCPUTime; typedef boost::shmem::vector<pair<unsigned long,ptime> ,boost::shmem::allocator<pair<unsigned long,ptime>,boost::shmem::named_shared_object::segment_manager > >
typedef boost::shmem::vector<pair<float,ptime> ,boost::shmem::allocator<pair<float,ptime>,boost::shmem::named_shared_object::segment_manager phshMemory;
struct PHSharedData { unsigned long TPM; phshCPUTime CPUTime; phshMemory Memory; }; PHSharedData phsd; phsd.CPUTime =phshCPUTime(_segment.get_segment_manager());
results in:
vector has no default constructor with shared memory allocators (because shared memory allocators have no default constructor). So put a constructor for PHSharedData struct PHSharedData { PHSharedData(segment_manager *mngr) : CPUTime(mngr) {} unsigned long TPM; phshCPUTime CPUTime; phshMemory Memory; };

Ion Gaztañaga wrote:
Jan Stetka wrote:
Hi,
Is something like this likely to work?
typedef interprocess::vector<int, ShmemAllocator> MyVector
typedef std::pair<const long, MyVector> ValueType;
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
typedef interprocess::map<long, MyVector, std::less<long>, ShmemAllocator> MyMap;
Yes, it should:
#include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/containers/map.hpp> #include <boost/interprocess/managed_shared_memory.hpp>
using namespace boost::interprocess;
typedef managed_shared_memory::segment_manager Mngr;
typedef allocator<int, Mngr> ShmemIntAllocator; typedef vector<int, ShmemIntAllocator> MyVector; typedef std::pair<const long, MyVector>ValueType; typedef allocator<ValueType, Mngr> ShmemValueTypeAllocator; typedef map<long, MyVector , std::less<long>, ShmemValueTypeAllocator> MyMap;
int main() { { shared_memory_object::remove("name"); managed_shared_memory shmem(open_or_create, "name", 65536);
MyMap *m = shmem.construct<MyMap>("Map name") (std::less<long>(), shmem.get_segment_manager());
//This is the usual way ValueType v(0, MyVector(shmem.get_segment_manager())); m->insert(v); //This is more efficient(data is moved instead of copied) //Note that "long" is not "const long" std::pair<long, MyVector> mv (1, MyVector(shmem.get_segment_manager())); m->insert(move(mv));
//Even more efficient, in-place construction: //the first argument is for the key (long), //the rest are for the mapped type m->emplace(2, shmem.get_segment_manager()); } shared_memory_object::remove("name"); return 0; } _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Does every instance of a container need its own segment?

Ion Gaztañaga wrote:
Jan Stetka wrote:
Does every instance of a container need its own segment?
No, a segment is shared between all containers that obtain a pointer to the same get_segment_manager().
Ion
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
I should have read the section in the documentation on containers of containers!
participants (3)
-
Ion Gaztañaga
-
Jan Stetka
-
Jan Stetka