3

我的目标是创建一个名为 SharedMemory 的模板单例类,它可以使用 boost::interprocess::managed_shared_memory 将给定的数据结构存储在共享内存中的映射中。

#ifndef SHARED_MEMORY_H_
#define SHARED_MEMORY_H_

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <string>
#include <utility>
#include <map>

using namespace boost::interprocess;

template<typename T>
class SharedMemory {
  typedef std::pair<std::string, T> ValueType;
  typedef allocator<ValueType, managed_shared_memory::segment_manager>
    ShmemAllocator;
  typedef map<std::string, T, std::less<std::string>, ShmemAllocator> SharedMap;

  public:
    static SharedMemory<T>& instance();
    void Create();
    void Destory();
    void insert(T* t);
    std::map<std::string, T> getMapOfRecords();
  private:
  SharedMemory();
  ~SharedMemory(){delete m_segment;}
  void Initialize();
  managed_shared_memory* m_segment;
  std::size_t m_size;
};

template<typename T>
inline void SharedMemory<T>::Create() {
  Destory();
  m_segment = new  managed_shared_memory(create_only, T::memory(), m_size);
  ShmemAllocator alloc_inst (m_segment->get_segment_manager());
  m_segment->construct<SharedMap>("SageMap")(std::less<std::string>(), alloc_inst);
}

template<typename T>
inline void SharedMemory<T>::Destory() {
  shared_memory_object::remove(T::memory());
}

template<typename T>
inline SharedMemory<T>& SharedMemory<T>::instance() {
  static SharedMemory<T> instance;
  return instance;
}

template<typename T>
inline SharedMemory<T>::SharedMemory()
    : m_size(65536) {

}

template<typename T>
inline void SharedMemory<T>::insert(T* t) {
  SharedMap* mymap = m_segment->find<SharedMap>("SageMap").first;
  mymap->insert(std::pair<std::string, T>(t->key(), *t));
}

template<typename T>
inline std::map<std::string, T> SharedMemory<T>::getMapOfRecords() {
  SharedMap* mymap = m_segment->find<SharedMap>("SageMap").first;
  return std::map<std::string, T>(mymap->begin(), mymap->end());
}
#endif

这是一个如何使用它的例子。

#include <boost/lexical_cast.hpp>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <functional>
#include <utility>
#include "SharedMemory.hpp"

struct simple_type {
  int i;
  std::string key() {return boost::lexical_cast<std::string>(i);}
  static const char* memory() {return std::string("simple_memory_page").c_str();}
  simple_type(int i): i(i){}
};

int main(int argc, char *argv[])
{
  if(argc == 1) {
    SharedMemory<simple_type>& test = SharedMemory<simple_type>::instance();
    test.Create();
    test.insert(new simple_type(1));
    test.insert(new simple_type(2));
    std::string s(argv[0]); s += " child ";
    if(0 != std::system(s.c_str()))
        return 1;
    test.Destory();
  } else {
    SharedMemory<simple_type>& test = SharedMemory<simple_type>::instance();
    std::map<std::string, simple_type> records = test.getMapOfRecords();
    for(auto it = records.begin(); it != records.end(); ++it) {
      std::cout << it->second.i << std::endl;
    }
  }
  return 0;
}

这是一个堆栈跟踪:

position_monitor_eu.exe!boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > >::get_pointer()  Line 81 + 0x1a bytes    C++
position_monitor_eu.exe!boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > >::get()  Line 153 + 0x16 bytes   C++
position_monitor_eu.exe!boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > >::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > >(const boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > > & ptr={...})  Line 117 + 0x16 bytes   C++
position_monitor_eu.exe!boost::intrusive::compact_rbtree_node_traits_impl<boost::interprocess::offset_ptr<void> >::get_left(boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > const > n={...})  Line 124 + 0x17 bytes   C++
position_monitor_eu.exe!boost::intrusive::rbtree_impl<boost::intrusive::setopt<boost::intrusive::detail::base_hook_traits<boost::container::containers_detail::rbtree_node<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type>,boost::interprocess::offset_ptr<void> >,boost::intrusive::rbtree_node_traits<boost::interprocess::offset_ptr<void>,1>,0,boost::intrusive::default_tag,3>,boost::container::containers_detail::node_compare<boost::container::containers_detail::value_compare_impl<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,simple_type>,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,boost::container::containers_detail::select1st<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,simple_type> > >,boost::container::containers_detail::rbtree_node<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type>,boost::interprocess::offset_ptr<void> > >,unsigned int,1> >::begin()  Line 273 + 0x42 bytes   C++
position_monitor_eu.exe!boost::container::containers_detail::rbtree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,simple_type>,boost::container::containers_detail::select1st<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,simple_type> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,boost::interprocess::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type>,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void>,0>,boost::interprocess::iset_index> > >::begin()  Line 493 + 0x28 bytes  C++
position_monitor_eu.exe!boost::container::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,boost::interprocess::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type>,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void>,0>,boost::interprocess::iset_index> > >::begin()  Line 245 + 0x1a bytes   C++
position_monitor_eu.exe!SharedMemory<simple_type>::getMapOfRecords()  Line 68 + 0x1e bytes  C++
position_monitor_eu.exe!main(int argc=2, char * * argv=0x02b03db8)  Line 200 + 0xc bytes    C++
position_monitor_eu.exe!__tmainCRTStartup()  Line 555 + 0x19 bytes  C
position_monitor_eu.exe!mainCRTStartup()  Line 371  C
kernel32.dll!77003677()     
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
ntdll.dll!775cc002()    
ntdll.dll!775cbfd5()    

我当前的问题是程序崩溃调用mymap->begin()in getMapOfRecords()

4

2 回答 2

6

看起来您将std::string类型放在共享内存中,这不起作用,因为它会为自己分配非共享内存。您应该使用boost::interprocess::basic_string类型而不是 from <boost/interprocess/containers/string.hpp>这里有一个将字符串放入地图的示例。


为了回答您在上面的评论中提出的问题,string::c_str()字符串被破坏后返回的值变得无效。这意味着访问返回的指针memory()将导致未定义的行为。

于 2012-05-11T21:07:57.203 回答
1

补充一下 interjay 写的关于 string::c_str() 的内容,返回的值在第一次非常量成员调用字符串对象之后不能保证有效。因此,对 string::resize 的后续调用可能会使 string::c_str() 返回的值无效。

于 2012-05-21T19:22:45.227 回答