oneTBB
oneTBB copied to clipboard
Allocating Tbb Concurrent Hash Map on Shared memory
I'm trying to allocate concurrent_hash_map on Shared Memory using Boost shared memory allocator
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <tbb/concurrent_hash_map.h>
template<class T>
using ShmAlloc = boost::interprocess::allocator<T, boost::interprocess::managed_shared_memory::segment_manager>;
using Key = int;
using Value = int;
using DataType = std::pair<const Key, Value>;
using Table = tbb::concurrent_hash_map<Key, Value, tbb::tbb_hash_compare<Key>, ShmAlloc <DataType>>;
int main()
{
boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only, "TbbTable", 65536);
const ShmAlloc<DataType> shm_alloc(segment.get_segment_manager());
// Creating Table
Table* table = segment.construct<Table>("t1")(shm_alloc);
.
.
.
// In another process
// Finding Table
auto res = segment.find<Table>("t1");
Table* found_table = res.first;
}
but I got following error:
concurrent_hash_map.h:580:37: error: cannot convert ‘boost::interprocess::allocator<tbb::interface5::concurrent_hash_map<int, int, tbb::tbb_hash_compare<int>, boost::interprocess::allocator<std::pair<const int, int>, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >::node, boost::interprocess::segment_manager<char,
boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::pointer {aka boost::interprocess::offset_ptr<tbb::interface5::concurrent_hash_map<int, int, tbb::tbb_hash_compare<int>, boost::interprocess::allocator<std::pair<const int, int>, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >::node, long int, long
unsigned int, 0ul>}’ to ‘void*’ in initialization
void *ptr = a.allocate(1);
I think the problem is you are not using Allocator::pointer (an offset_ptr when using Boost allocator).
Can anyone help me?
You are right that the TBB containers do not use the allocator's pointer type. Changing that could be complicated, as pointers might be modified with atomic operations, reinterpreted as pointers of different type or as integers, etc. There are no plans to make such changes, though if someone sends us a patch we will look at it.
@thegreathir is this issue still relevant? Have you tried to use concurrent_hash_map
with latest version of TBB (oneTBB)?