|
Boost : |
From: Murali Kishore (bmuralikishore_at_[hidden])
Date: 2024-09-28 07:49:57
Thanks Ion. I have tried the following sample, I see one issue that "find"
is failing in the following sample.
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/unordered_map.hpp>
#include <boost/interprocess/containers/set.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <iostream>
// Define your types
using timer_id_t = int;
struct timer_instance_t {
int id; // Example member
};
// Define allocators for shared memory
typedef boost::interprocess::allocator<timer_instance_t,
boost::interprocess::managed_shared_memory::segment_manager> TimerAllocator;
typedef boost::interprocess::allocator<std::pair<const timer_id_t,
timer_instance_t>,
boost::interprocess::managed_shared_memory::segment_manager> MapAllocator;
// Define the unordered_map type
typedef boost::unordered_map<timer_id_t, timer_instance_t,
std::hash<timer_id_t>, std::less<timer_id_t>, MapAllocator>
timer_instance_map_t;
int main() {
using namespace boost::interprocess;
// Create managed shared memory
managed_shared_memory segment(open_or_create, "MySharedMemory", 65536);
// Construct the unordered map in shared memory
timer_instance_map_t* timer_instance_map =
segment.find_or_construct<timer_instance_map_t>("TimerInstanceMap")(10,
std::hash<timer_id_t>(), std::less<timer_id_t>(),
segment.get_segment_manager());
// Allocate a timer_instance_t in shared memory
timer_instance_t* instance =
segment.construct<timer_instance_t>("TimerInstance")();
// Use emplace to insert the timer_instance_t pointer into the map
timer_id_t id = 100;
timer_instance_map->emplace(id, *instance); // Copy the allocated
instance into the map
// Accessing the instance
auto it = timer_instance_map->find(id);
if (it != timer_instance_map->end()) {
std::cout << "Found timer instance with id: " << it->second.id <<
std::endl;
} else {
std::cout << "Timer instance not found." << std::endl;
}
for (const auto& pair : *timer_instance_map) {
std::cout <<"Timer Id: " << pair.first <<", Timer Instance Id: " <<
pair.second.id<< std::endl;
}
// Cleanup: You may want to remove the shared memory segment
shared_memory_object::remove("MySharedMemory");
return 0;
}
output:
Timer Instance not found.
Timer Id: 100, Timer Instance Id: 0
need help in understanding why find is failing.
Regards,
Murali Kishore
On Mon, Sep 9, 2024 at 8:42â¯PM Ion Gaztañaga <igaztanaga_at_[hidden]> wrote:
> El 09/09/2024 a las 15:18, Murali Kishore via Boost escribió:
> > Hi all,
> >
> > I see multiset is not present in boost interprocess, so I tried to use a
> > "set" with a customized comparator to achieve multiset functionality.
>
> You can use any Boost.Container container with Boost.Interprocess,
> including multiset.
>
> Boost.Interprocess own container aliases will be deprecated soon, since
> they only forward to Boost.Container. I'll maintain those container
> header inside Interprocess for backwards compatibility for several Boost
> releases but in the end they will be removed.
>
> Best,
>
> Ion
>
>
-- Regards, Murali Kishore
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk