Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/interprocess/shared_memory_object.hpp>
- #include <boost/interprocess/managed_shared_memory.hpp>
- #include <boost/interprocess/offset_ptr.hpp>
- #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
- #include <boost/interprocess/smart_ptr/weak_ptr.hpp>
- #include <boost/interprocess/allocators/allocator.hpp>
- #include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
- #include <boost/interprocess/containers/map.hpp>
- #include <boost/interprocess/containers/vector.hpp>
- #include <boost/multi_index_container.hpp>
- #include <boost/multi_index/ordered_index.hpp>
- #include <boost/multi_index/hashed_index.hpp>
- #include <boost/multi_index/member.hpp>
- #include <boost/multi_index/mem_fun.hpp>
- using boost::interprocess::managed_shared_memory;
- using boost::interprocess::allocator;
- using boost::interprocess::managed_shared_ptr;
- using boost::interprocess::managed_weak_ptr;
- using boost::interprocess::anonymous_instance;
- using boost::interprocess::make_managed_shared_ptr;
- typedef managed_shared_memory::segment_manager segment_manager_t;
- typedef allocator<void, segment_manager_t> void_allocator;
- typedef long long idType;
- typedef unsigned long long releaseType;
- // Define a template SharableDataTypes
- template<class T, class Key=int> struct SharableDataTypes {
- typedef typename managed_shared_ptr<T, managed_shared_memory>::type SharedPtrType;
- typedef typename managed_weak_ptr <T, managed_shared_memory>::type WeakPtrType;
- typedef allocator<WeakPtrType, segment_manager_t> WeakPtrAllocatorType;
- typedef boost::interprocess::vector<WeakPtrType , WeakPtrAllocatorType> WeakPtrVectorType;
- };
- class Data{
- private:
- idType id;
- releaseType creationRelease;
- public:
- Data(idType i, releaseType r){ id=i; creationRelease=r;}
- const idType getObjectId() const {return id;}
- const releaseType getCreationRelease() const {return creationRelease;}
- // Define type of container storing this kind of data
- typedef boost::multi_index_container<
- SharableDataTypes<Data>::SharedPtrType,
- boost::multi_index::indexed_by<
- boost::multi_index::hashed_unique<BOOST_MULTI_INDEX_CONST_MEM_FUN(Data, const idType, getObjectId)>
- ,boost::multi_index::ordered_non_unique< boost::multi_index::tag<releaseType>, BOOST_MULTI_INDEX_CONST_MEM_FUN(Data, const releaseType, getCreationRelease)>
- >,
- boost::interprocess::allocator<SharableDataTypes<Data>::SharedPtrType, segment_manager_t>
- > DataContainerType;
- };
- class DataVectorContainer{
- private:
- SharableDataTypes<Data>::WeakPtrVectorType dataWPVector;
- public:
- DataVectorContainer(void_allocator * alloc) : dataWPVector(*alloc){};
- void addData (const SharableDataTypes<Data>::SharedPtrType & PData){
- unsigned int sizeBefore = dataWPVector.size();
- unsigned int capacityBefore = dataWPVector.capacity();
- dataWPVector.push_back(PData);
- // std::cerr << "adddataWPVectorElem called with PData.id =" << PData->getObjectId() << " from thread " << getpid() << ":" << pthread_self() << std::endl;
- unsigned int sizeAfter = dataWPVector.size();
- unsigned int capacityAfter = dataWPVector.capacity();
- if (dataWPVector.rbegin()->lock()->getObjectId() != PData->getObjectId()){
- std::cerr << "After pushback in dataWPVector vector last elem Id=" << dataWPVector.rbegin()->lock()->getObjectId() << " added cable Id =" << PData->getObjectId() << std::endl;
- std::cerr << "--> sizeBefore=" << sizeBefore << ", capacityBefore=" << capacityBefore << ", sizeAfter=" << sizeAfter<< ", capacityAfter=" << capacityAfter << std::endl;
- printData();
- exit(1);
- }else{
- if (capacityBefore != capacityAfter){
- std::cerr << "After pushback in dataWPVector capacity changed from " << capacityBefore << " to " << capacityAfter << " without any damage." << std::endl;
- // printData();
- }
- }
- }
- void printData(){
- std::ostringstream oss;
- oss << "dataWPVector[" << std::endl;
- for (SharableDataTypes<Data>::WeakPtrVectorType::iterator cableIt = dataWPVector.begin(); cableIt != dataWPVector.end(); ++cableIt){
- oss << cableIt->lock()->getObjectId() << ", ";
- }
- oss << "]" << std::endl;
- std::cerr << oss.str();
- }
- };
- int main(int argc, char **argv) {
- boost::interprocess::shared_memory_object::remove("testPushBackVector");
- managed_shared_memory * managedSharedMemorySegment = new managed_shared_memory(boost::interprocess::open_or_create, "testPushBackVector", 1024*1024*10);
- void_allocator * managedSharedMemoryAllocator = new void_allocator(managedSharedMemorySegment->get_segment_manager());
- Data::DataContainerType * dataContainer = managedSharedMemorySegment->construct<Data::DataContainerType >
- (anonymous_instance)(
- Data::DataContainerType::ctor_args_list(),
- Data::DataContainerType::allocator_type(managedSharedMemorySegment->get_segment_manager())
- );
- SharableDataTypes<DataVectorContainer>::SharedPtrType myDataVectorContainer = make_managed_shared_ptr(managedSharedMemorySegment->
- construct<DataVectorContainer>(anonymous_instance)(managedSharedMemoryAllocator), *managedSharedMemorySegment);
- for (unsigned int i = 1; i <= 10000; ++i){
- SharableDataTypes<Data>::SharedPtrType myData = make_managed_shared_ptr(managedSharedMemorySegment->
- construct<Data>(anonymous_instance)(2*i, i), *managedSharedMemorySegment);
- dataContainer->insert(myData);
- myDataVectorContainer->addData(myData);
- }
- // myDataVectorContainer->printData();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement