【问题标题】:How to construct vector with given count in boost interprocess and add elements to it如何在提升进程间构造具有给定计数的向量并向其添加元素
【发布时间】:2020-01-31 15:55:55
【问题描述】:

我已经开始学习 boost 进程间库,目前遇到了 2 个问题。第一个与向量的构造有关,并传递其大小。

shm.construct<MyShmVector>("MyVector")(MAX_INPUT_SIZE, std::make_pair{"not_used", 0.0}, allocInstance);

/usr/include/boost169/boost/container/string.hpp:220:22: error: no matching function for call to 'boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()'
      : Allocator()

所以我应该以其他方式将参数传递给向量吗?

第二个问题与emplace_back 函数有关。当我尝试以这种方式向矢量添加元素时,出现错误:

/usr/include/boost169/boost/container/allocator_traits.hpp:415:10: error: no matching function for call to 'std::pair<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >, double>::pair(std::basic_string<char>, double)'
   {  ::new((void*)p, boost_container_new_t()) T(::boost::forward<Args>(args)...); }
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

所以我又做错了什么?

代码:

#include<boost/interprocess/managed_shared_memory.hpp>
#include<boost/interprocess/containers/vector.hpp>
#include<boost/interprocess/containers/string.hpp>
#include<boost/interprocess/allocators/allocator.hpp>

constexpr int MAX_INPUT_SIZE = 10'000;
namespace bip = boost::interprocess;

class ShmWrapper
{
    using SegmentManagerType = bip::managed_shared_memory::segment_manager;
    using CharAllocator = bip::allocator<char, SegmentManagerType>;
    using MyShmString =  bip::basic_string<char, std::char_traits<char>, CharAllocator>;
    using MyShmPair = std::pair<MyShmString, double>;
    using MyShmPairAllocator = bip::allocator<MyShmPair, SegmentManagerType>;
    using MyShmVector = bip::vector<MyShmPair, MyShmPairAllocator>;
public:
    ShmWrapper()
    {
        bip::shared_memory_object::remove("SHM_SEGMENT");
        bip::managed_shared_memory shm{bip::open_or_create, "SHM_SEGMENT", MAX_INPUT_SIZE * sizeof(MyShmPair) * 4 + sizeof(MyShmVector)};

        MyShmPairAllocator const allocInstance{shm.get_segment_manager()};
        stocks_ = shm.construct<MyShmVector>("MyVector")(MAX_INPUT_SIZE, std::make_pair("not_used", 0.0), allocInstance);

        for (auto index = 1; index < MAX_INPUT_SIZE; ++index)
            stocks_->emplace_back("Item_" + std::to_string(index), 0.0);
    }

private:
    MyShmVector* stocks_;
};

int main()
{
    ShmWrapper shm;
    return 0;
}

【问题讨论】:

    标签: c++ boost allocator interprocess boost-interprocess


    【解决方案1】:

    我在试用您的代码时根本没有收到第一个错误:

    https://godbolt.org/z/rBBfHe

    这是您正在使用的普通构造函数,它也存在于 std::vector 中,因此我知道您应该没有其他调用方式。


    我确实复制了第二个。它抱怨您使用std::to_stringstd::string 输出写入boost::container::basic_string 的参数。

    boost::container::basic_string 有点像 C++98 的遗物。根据文档,它与许多第 3 方“更好的字符串”实现一样,是为了避免 gcc 的 std::string 中的写时复制语义并添加一些性能优化。

    由于 C++11 禁止写时复制并要求进行短字符串优化,因此没有理由不使用 std::string。只需更改此行:

    using MyShmString =  bip::basic_string<char, std::char_traits<char>, CharAllocator>;
    

    到这里:

    using MyShmString = std::string
    

    它应该可以编译。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-18
      • 2020-08-06
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      相关资源
      最近更新 更多