I've started to learn boost interprocess library and I encountered 2 problems so far. The first one is related to construction of vector with passing its size.
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()
so should I pass arguments to the vector in other way ?
The second problem is related to emplace_back function. When I try to add in this way an element to the vector I get an error:
/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)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So once again I did something wrong ?
The code:
#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;
}
I simply don't get the first error when I try out your code:
That's a normal constructor you're using, and it exists in std::vector
too, so there's not another way you're supposed to be calling that I know of.
I did reproduce the second one. It's complaining that you used the std::string
output of std::to_string
to write to an argument that is boost::container::basic_string
.
boost::container::basic_string
is a bit of a C++98 relic. According to the documentation it, like a lot of 3rd party "better string" implementations, was made to avoid the copy-on-write semantics in gcc's std::string
and add some performance optimizations.
Since C++11 bans copy-on-write and mandates short-string optimization, there's few reasons not to use std::string
. Just change this line:
using MyShmString = bip::basic_string<char, std::char_traits<char>, CharAllocator>;
to this:
using MyShmString = std::string
And it should compile.