Boost logo

Boost Users :

Subject: [Boost-users] [Interprocess] updating elements in vector of maps fails in optimized code
From: Steven Maenhout (Steven.Maenhout_at_[hidden])
Date: 2011-05-11 07:01:53


I'm using Boost 1.46 on Linux x86_64 in combination with gcc version
4.5.1. I'm trying to implement a a sparse matrix in a shared memory
segment as a vector of maps. When trying to update previously inserted
elements of the matrix, at a certain point, the program either crashes
or stalls indefinitely. This only occurs if the gcc optimization is set
higher than O0. If compiler optimizations are completely turned off (so
using the -O0 switch) the program works as expected.

Below, I provide some trimmed code that demonstrates the problem. I
would be very grateful if somebody could indicate me what's causing
this erratic behavior.

Steven

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

#define MATSIZE 5

typedef boost::interprocess::managed_shared_memory::segment_manager
SegmentManager_t;
typedef boost::interprocess::allocator<void , SegmentManager_t>
VoidAllocator_t;
typedef std::pair<int,double> MapValue_t;
typedef boost::interprocess::allocator<MapValue_t , SegmentManager_t>
MapValueAllocator_t;
typedef
boost::interprocess::map<int,double,std::less<int>,MapValueAllocator_t>
Map_t;
typedef boost::interprocess::allocator<Map_t,SegmentManager_t>
MapAllocator_t;
typedef boost::interprocess::vector<Map_t,MapAllocator_t> VectorofMaps_t;

int main(int argc, char **argv)
{
   VectorofMaps_t::size_type i;
   int j;
   Map_t::iterator mapiter;
   try
   {
boost::interprocess::shared_memory_object::remove("MySharedMemory");
     boost::interprocess::managed_shared_memory
mfile(boost::interprocess::create_only,"MySharedMemory",65536);
     VoidAllocator_t alloc_inst (mfile.get_segment_manager());
     VectorofMaps_t*
pS=mfile.construct<VectorofMaps_t>(boost::interprocess::anonymous_instance)(MATSIZE,Map_t(std::less<int>(),alloc_inst),alloc_inst);

      for(i=0;i<MATSIZE;++i)
      {
        for(j=0;j<MATSIZE;++j)
         (*pS)[i].insert(std::pair<int,double>(j,1.0));
      }
      for(i=0;i<MATSIZE;++i)
      {
       for(j=0;j<MATSIZE;++j)
       {
         mapiter=(*pS)[i].find(j);
        if(mapiter!=(*pS)[i].end())
         mapiter->second+=1.0;
        else
          (*pS)[i].insert(std::pair<int,double>(j,1.0));
       }
      }
      for(i=0;i<MATSIZE;++i)
      {
        for(mapiter=(*pS)[i].begin();mapiter!=(*pS)[i].end();++mapiter)
          std::cout<<mapiter->second<<" ";
        std::cout<<std::endl;
      }
      boost::interprocess::shared_memory_object::remove("MySharedMemory");
   }
   catch(boost::interprocess::interprocess_exception e)
   {
     std::cerr<<"Interprocess error: "<<e.what();
   }
   catch(std::exception e)
   {
     std::cerr<<"Standard exception: "<<e.what();
   }
   catch(...)
   {
     std::cerr<<"unknown error";
   }
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net