如何在boost进程间构建具有给定计数的向量并向其添加元素
How to construct vector with given count in boost interprocess and add elements to it
我开始学习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;
}
当我尝试你的代码时,我根本没有得到第一个错误:
这是您正在使用的普通构造函数,它也存在于 std::vector
中,因此据我所知,没有其他您应该调用的方法。
我确实复制了第二个。它抱怨您使用 std::to_string
的 std::string
输出写入一个参数 boost::container::basic_string
.
boost::container::basic_string
有点像 C++98 的遗物。根据文档,它与许多第 3 方 "better string" 实现一样,是为了避免 gcc std::string
中的写时复制语义并添加一些性能优化。
由于 C++11 禁止写时复制并要求进行短字符串优化,因此没有理由不使用 std::string
。只需更改此行:
using MyShmString = bip::basic_string<char, std::char_traits<char>, CharAllocator>;
对此:
using MyShmString = std::string
它应该编译。
我开始学习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;
}
当我尝试你的代码时,我根本没有得到第一个错误:
这是您正在使用的普通构造函数,它也存在于 std::vector
中,因此据我所知,没有其他您应该调用的方法。
我确实复制了第二个。它抱怨您使用 std::to_string
的 std::string
输出写入一个参数 boost::container::basic_string
.
boost::container::basic_string
有点像 C++98 的遗物。根据文档,它与许多第 3 方 "better string" 实现一样,是为了避免 gcc std::string
中的写时复制语义并添加一些性能优化。
由于 C++11 禁止写时复制并要求进行短字符串优化,因此没有理由不使用 std::string
。只需更改此行:
using MyShmString = bip::basic_string<char, std::char_traits<char>, CharAllocator>;
对此:
using MyShmString = std::string
它应该编译。