我正在尝试使用Boost共享内存分配器在共享内存上分配Folly Concurrent Hash Map。
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <folly/concurrency/ConcurrentHashMap.h>
template<class T>
using ShmAlloc = boost::interprocess::allocator<T,
boost::interprocess::managed_shared_memory::segment_manager>;
using Key = int;
using Value = int;
using Table = folly::ConcurrentHashMap<Key, Value, std::hash<Key>,
std::equal_to<Key>, ShmAlloc<uint8_t >>;
int main()
{
//Create a new segment with given name and size
managed_shared_memory segment(create_only, "SharedTable", 65536);
//Initialize shared memory STL-compatible allocator
ShmAlloc<uint8_t> alloc_inst (segment.get_segment_manager());
Table* table = segment.construct<Table>("t1")(alloc_inst);
// In another process I want to find it
auto res = segment.find<Table>("t1");
Table* found_table = res.first;
}
但出现以下错误:
ConcurrentHashMap.h:496:74: error: no matching function for call to ‘boost::interprocess::allocator<unsigned char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()’
SegmentT* newseg = (SegmentT*)Allocator().allocate(sizeof(SegmentT));
我尝试用具有默认构造函数和存储Boost共享内存实例的静态成员的分配器包装Boost共享内存分配器。这样可以解决编译错误,但会引起严重的以下问题:由于并发哈希映射未使用Allocator :: pointer(使用Boost分配器时为offset_ptr)。
>有人可以帮助我吗?