I'm using shared memory from boost library in C++, i'm trying to allocate an unordered_map to share with other process. The code of server is the below:
MapCreator.h
//#pragma once
#pragma warning( disable :4494 )
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
struct dataClass {
double Somma;
int Contatore;
};
namespace bip = boost::interprocess;
namespace bc = boost::container;
#ifdef COLIRU
using Segment = bip::managed_mapped_file;
#else
using Segment = bip::managed_shared_memory;
#endif
using Mgr = Segment::segment_manager;
template <typename T> using Alloc = bip::allocator<T, Mgr>;
using MyString = bc::basic_string<char, std::char_traits<char>, Alloc<char>>;
using KeyType = MyString;
using MappedType = dataClass;
using ValueType = std::pair<KeyType const, MappedType>;
using MySHMMap = boost::unordered_map<KeyType, MappedType, boost::hash<MyString>,
std::equal_to<MyString>, Alloc<ValueType>>;
class MapCreator {
public:
static constexpr int sizeDeclared = 1324*1024*1024; //< Here the problem, if i set 2000*1024*1024, the client application throw error
MapCreator(const char* Nome) // : nameMemory(Nome)
{
nameMemory = Nome;
remove();
segment = Segment{ bip::create_only, nameMemory, sizeDeclared };
mappa = segment.find_or_construct<MySHMMap>("MySHMMapName")(segment.get_segment_manager());
}
dataClass getValue(std::string key) const {
return mappa->at(MyString(key.c_str(), segment.get_segment_manager()));
}
void insertValue(std::string key,dataClass value) {
mappa->emplace(MyString(key.c_str(), segment.get_segment_manager()),
value);
}
double getFreeMemory() {
return ((double)segment.get_free_memory() / 1024 / 1024 / 1024);
}
long getSize() {
return mappa->size();
}
void remove() {
bip::shared_memory_object::remove(nameMemory);
}
double getTotalSize() {
return (double)sizeDeclared/1024/1024/1024;
}
double getTotalMemory() {
return (double)segment.get_size() / 1024 / 1024 / 1024;
}
private:
// note: declaration order defines initialization order!
const char* nameMemory = "SharedMemoryName";
Segment segment;//{ bip::open_or_create, nameMemory, sizeDeclared };
MySHMMap* mappa = nullptr;
};
Meanwhile the main code is this:
#include "MapCreator.h"
int main(){
MapCreator mappaClass("thread1");
mappaClass.insertValue("a", dataClass{ 3.12,2123 });
}
The code of Reader is the below:
ReaderFromMemory.h
//#pragma once
#pragma warning( disable :4494 )
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
struct dataClass {
double Somma;
int Contatore;
};
namespace bip = boost::interprocess;
namespace bc = boost::container;
#ifdef COLIRU
using Segment = bip::managed_mapped_file;
#else
using Segment = bip::managed_shared_memory;
#endif
using Mgr = Segment::segment_manager;
template <typename T> using Alloc = bip::allocator<T, Mgr>;
using MyString = bc::basic_string<char, std::char_traits<char>, Alloc<char>>;
using KeyType = MyString;
using MappedType = dataClass;
using ValueType = std::pair<KeyType const, MappedType>;
using MySHMMap = boost::unordered_map<KeyType, MappedType, boost::hash<MyString>,
std::equal_to<MyString>, Alloc<ValueType>>;
class Reader {
public:
Reader() : mappa(segment.find_or_construct<MySHMMap>("MySHMMapName")(
segment.get_segment_manager()))
{
}
dataClass getValue(const char* key) const {
return mappa->at(MyString(key, segment.get_segment_manager())); // < Here is the error while reading
}
private:
// note: declaration order defines initialization order!
static constexpr char const* nameMemory = "thread1";
Segment segment{ bip::open_only, nameMemory };;
MySHMMap* mappa = nullptr;
};
This is the main:
#include "ReaderFromMemory.h"
int main(){
Reader reader;
auto testValue = reader.getValue("a");
}
So the problem is allocate and read more than 2GB.
I already tried to use /LARGEADDRESSAWARE flag but for the Reader doesn't work I'm using:
Visual Studio 2022 for compile in x86 mode because the reader is a DLL that be used by VB6
boost library version 1.78.0