在共享内存中插入映射时出现编译器错误

问题描述 投票:2回答:1

我正在尝试创建一个存储在共享内存中的映射矩阵(本质上是一个map<int, map<int, int>>)。我正在使用boost并遵循以下答案的示例:https://stackoverflow.com/a/33913753/5760608

但是,我无法对此进行编译。我收到了我无法解析的超长错误。

使用以下命令编译:clang -std=c++11 -o shmem_sparse_matrix shmem_sparse_matrix.cpp -lpthread -lrt -lstdc++ -lm

此外,如果您知道可以立即使用的矩阵共享内存实现,请告诉我-我找不到。

#include <iostream>
#include <exception>
#include <string>

#include <boost/interprocess/containers/map.hpp>
#include <boost/container/scoped_allocator.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

using namespace boost::interprocess;

static const std::string SHMEM_NAME = "MySharedMemorySegment";

namespace Shared {
    typedef managed_shared_memory                              Segment;
    typedef Segment::segment_manager                           SegmentManager;
    typedef int                                                KeyType;

    template <typename T>
    using Alloc  = allocator<T, SegmentManager>;
    using Scoped = boost::container::scoped_allocator_adaptor<Alloc<int>>;

    typedef map<int, int, Scoped>                              Row;
    typedef map<int, Row, Scoped>                              Matrix;
}

class ShmemTest {
private:
    Shared::Segment *segment;
    Shared::Matrix *matrix;
    size_t shmem_block = 10000;
    struct shm_remove {
        shm_remove()  { shared_memory_object::remove(SHMEM_NAME.data()); }
        ~shm_remove() { shared_memory_object::remove(SHMEM_NAME.data()); }
    } remover;

public:
    ShmemTest() {
        segment = new managed_shared_memory(create_only, SHMEM_NAME.data(), shmem_block);
    }

    void create_matrix() {
        matrix = segment->construct<Shared::Matrix>("SparseMatrix")(segment->get_segment_manager());
    }

    void insert_element(int i, int j, int k) {
        Shared::Row &row = (*matrix)[i];
        row[j] = k;
    }
};

int main() {
    ShmemTest shmem_test;
    shmem_test.create_matrix();
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            shmem_test.insert_element(i, j, i);
        }
    }
}

编译器输出TLDR(仅显示错误):

clang -std=c++11 -o shmem_sparse_matrix shmem_sparse_matrix.cpp -lpthread -lrt -lstdc++ -lm

/usr/local/include/boost/container/detail/tree.hpp:464:14: error: type 'const key_compare' (aka 'const boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >') does not provide a call operator
   {  return this->key_comp()(key1, this->key_from(nonkey2));  }

/usr/local/include/boost/container/detail/tree.hpp:468:14: error: type 'const key_compare' (aka 'const boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >') does not provide a call operator
   {  return this->key_comp()(this->key_from(nonkey1), key2);  }

/usr/local/include/boost/intrusive/detail/tree_value_compare.hpp:98:14: error: type 'const key_compare' (aka 'const boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >') does not provide a call operator
   {  return this->key_comp()(KeyOfValue()(value1), KeyOfValue()(value2));  }

/usr/local/include/boost/container/scoped_allocator.hpp:443:4: error: constructor for 'boost::container::dtl::scoped_allocator_adaptor_base<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> >>' must explicitly initialize the base class 'boost::interprocess::allocator<int,
      boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> >' which does not have a default constructor
   scoped_allocator_adaptor_base()

shmem_sparse_matrix.cpp:47:37: note: in instantiation of member function 'boost::container::map<int, boost::container::map<int, int, boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int,
      boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, void, void>,
      boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long,
      unsigned long, 0>, 0>, iset_index> > >, void, void>::operator[]' requested here
        Shared::Row &row = (*matrix)[i];

/usr/local/include/boost/container/detail/tree.hpp:464:14: error: type 'const key_compare' (aka 'const boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >') does not provide a call operator
   {  return this->key_comp()(key1, this->key_from(nonkey2));  }

/usr/local/include/boost/container/detail/tree.hpp:468:14: error: type 'const key_compare' (aka 'const boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >') does not provide a call operator
   {  return this->key_comp()(this->key_from(nonkey1), key2);  }

/usr/local/include/boost/intrusive/detail/tree_value_compare.hpp:98:14: error: type 'const key_compare' (aka 'const boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >') does not provide a call operator
   {  return this->key_comp()(KeyOfValue()(value1), KeyOfValue()(value2));  }

shmem_sparse_matrix.cpp:48:12: note: in instantiation of member function 'boost::container::map<int, int, boost::container::scoped_allocator_adaptor<boost::interprocess::allocator<int, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, void, void>::operator[]' requested here
        row[j] = k;

完整的编译器输出:https://gist.github.com/ffrankies/80114b648e9d88c2fd26e361c8b34111

c++ boost shared-memory boost-interprocess
1个回答
0
投票

您需要正确定义typedef:

typedef managed_shared_memory                              Segment;
typedef Segment::segment_manager                           SegmentManager;
typedef int                                                KeyType;
typedef map<int, int> MappedType;
typedef std::pair<const KeyType, MappedType>               ValueType;

typedef allocator<ValueType, SegmentManager>               ShmemAllocator;

typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> Matrix;

然后在insert_element()中进行少许更改以使用自动:auto &row = (*matrix)[i];,这应该可以工作。下面是完整的工作示例:

#include <iostream>
#include <exception>
#include <string>

#include <boost/interprocess/containers/map.hpp>
#include <boost/container/scoped_allocator.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

using namespace boost::interprocess;

static const std::string SHMEM_NAME = "MySharedMemorySegment";

namespace Shared {
    typedef managed_shared_memory                              Segment;
    typedef Segment::segment_manager                           SegmentManager;
    typedef int                                                KeyType;
    typedef map<int, int> MappedType;
    typedef std::pair<const KeyType, MappedType> ValueType;

    typedef allocator<ValueType, SegmentManager> ShmemAllocator;

    typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> Matrix;
}

class ShmemTest {
private:
    Shared::Segment *segment;
    Shared::Matrix *matrix;
    size_t shmem_block = 10000;
    struct shm_remove {
        shm_remove()  { shared_memory_object::remove(SHMEM_NAME.data()); }
        ~shm_remove() { shared_memory_object::remove(SHMEM_NAME.data()); }
    } remover;

public:
    ShmemTest() {
        segment = new managed_shared_memory(create_only, SHMEM_NAME.data(), shmem_block);
    }

    void create_matrix() {
        matrix = segment->construct<Shared::Matrix>("SparseMatrix")(segment->get_segment_manager());
    }

    void insert_element(int i, int j, int k) {
        auto &row = (*matrix)[i];
        row[j] = k;
    }
};

int main() {
    ShmemTest shmem_test;
    shmem_test.create_matrix();
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            shmem_test.insert_element(i, j, i);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.