[shmem] problem retrieving objects from map
Hello,
I have a problem retrieving objects from a map placed in shared
memory. I have two processes:
proc#1: creates the shared memory, puts a map into it and populates it
with objects
proc#2: connects to the shared memory and tries to read out the
objects from the map
Whenever I try to read out the objects from the map, I get zeroes for
the internal members (a, b).
The class definition for the objects:
--------------------------------------------------
class ShmNode
{
private:
int a;
int b;
public:
ShmNode();
ShmNode(int newa, int newb);
~ShmNode();
void set(int newa, int newb);
int getA();
int getB();
};
--------------------------------------------------
I'm posting the source codes for the two programs. I removed the
synchronization stuff with semaphores for the sake of clarity:
Proc#1:
--------------------------------------------------
#include
The problem is you are storing pointers in the map, which are not in
the address space of proc#2. Check
http://ice.prohosting.com/newfunk/boost/libs/shmem/doc/html/shmem/limitation...
On 8/1/06, Pataki Szilard
Hello,
I have a problem retrieving objects from a map placed in shared memory. I have two processes:
proc#1: creates the shared memory, puts a map into it and populates it with objects proc#2: connects to the shared memory and tries to read out the objects from the map
Whenever I try to read out the objects from the map, I get zeroes for the internal members (a, b).
The class definition for the objects: -------------------------------------------------- class ShmNode { private: int a; int b; public: ShmNode(); ShmNode(int newa, int newb); ~ShmNode(); void set(int newa, int newb); int getA(); int getB(); }; --------------------------------------------------
I'm posting the source codes for the two programs. I removed the synchronization stuff with semaphores for the sake of clarity:
Proc#1: -------------------------------------------------- #include
#include #include #include #include #include <iostream> #include "../shmem/ShmNode.hpp"
typedef std::pair< const int, ShmNode*> MyPair;
typedef boost::shmem::allocator < MyPair,
boost::shmem::named_shared_object::segment_manager> MyAlloc;
typedef boost::shmem::map< const int, ShmNode*, std::less<const int>, MyAlloc> MyMap;
int main() { boost::shmem::named_shared_object segment;
segment.create("/myshmem", 20000);
MyAlloc nodesAlloc(segment.get_segment_manager()); std::less<const int> comparator;
MyMap *table = segment.construct<MyMap>("table")(comparator, nodesAlloc);
MyPair myPair1(1, new ShmNode(1,2)); MyPair myPair2(2, new ShmNode(3,4)); MyPair myPair3(3, new ShmNode(5,6));
table->insert(myPair3); table->insert(myPair1); table->insert(myPair2);
return 0; } --------------------------------------------------
Proc#2 -------------------------------------------------- /* same includes */
/* same typedefs */
int main() { boost::shmem::named_shared_object segment; segment.open("/myshmem");
MyMap *table = segment.find<MyMap>("table").first;
std::cout << "table size: " << table->size() << std::endl;
MyMap::iterator iter; for (iter = table->begin(); ; iter++) { if (iter == table->end()) break; std::cout << iter->second->getA() << " " << iter->second->getB() << std::endl; } return 0; } --------------------------------------------------
The problem once again: in proc#2, iter->second->getA() and iter->second->getB() return 0. If I put the same loop into proc#1, the correct values are returned. As a further hint, the size of the map object in proc#2 is returned correctly (=3).
What am I doing wrong?
Regards, Szilard _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Yeah, using pointers in that way is not something i've tried. Does the synchronization code you've removed stop the first program exiting before the second?
Hi!
Thanks for answering! Figured it out in the end...
-------------------------------------------
typedef std::pair<
const int,
ShmNode>
MyPair;
typedef boost::shmem::allocator <
MyPair,
boost::shmem::named_shared_object::segment_manager>
MyAlloc;
typedef boost::shmem::map<
const int,
ShmNode,
std::less<const int>,
MyAlloc>
MyMap;
(...)
ShmNode shmNode1(1, 2);
ShmNode shmNode2(3, 4);
ShmNode shmNode3(5, 6);
MyPair myPair1(1, shmNode1);
MyPair myPair2(2, shmNode2);
MyPair myPair3(3, shmNode3);
table->insert(myPair3);
table->insert(myPair1);
table->insert(myPair2);
-------------------------------------------
Still, can anybody explain what's the purpose of "comparator" and
"nodesAlloc" in the following piece of code? I found no documentation
whatsoever regarding this, and I managed to compile it by pure
accident. Why is there a need for a second comparator when one was
already defined for MyMap. Same story with the allocator...
-------------------------------------------
MyAlloc nodesAlloc(segment.get_segment_manager());
std::less<const int> comparator;
MyMap *table = segment.construct<MyMap>("table")(comparator, nodesAlloc);
-------------------------------------------
Regards,
Szilard
On 8/1/06, Adam Lewis
The problem is you are storing pointers in the map, which are not in the address space of proc#2. Check http://ice.prohosting.com/newfunk/boost/libs/shmem/doc/html/shmem/limitation...
On 8/1/06, Pataki Szilard
wrote: Hello,
I have a problem retrieving objects from a map placed in shared memory. I have two processes:
proc#1: creates the shared memory, puts a map into it and populates it with objects proc#2: connects to the shared memory and tries to read out the objects from the map
Whenever I try to read out the objects from the map, I get zeroes for the internal members (a, b).
The class definition for the objects: -------------------------------------------------- class ShmNode { private: int a; int b; public: ShmNode(); ShmNode(int newa, int newb); ~ShmNode(); void set(int newa, int newb); int getA(); int getB(); }; --------------------------------------------------
I'm posting the source codes for the two programs. I removed the synchronization stuff with semaphores for the sake of clarity:
Proc#1: -------------------------------------------------- #include
#include #include #include #include #include <iostream> #include "../shmem/ShmNode.hpp"
typedef std::pair< const int, ShmNode*> MyPair;
typedef boost::shmem::allocator < MyPair,
boost::shmem::named_shared_object::segment_manager> MyAlloc;
typedef boost::shmem::map< const int, ShmNode*, std::less<const int>, MyAlloc> MyMap;
int main() { boost::shmem::named_shared_object segment;
segment.create("/myshmem", 20000);
MyAlloc nodesAlloc(segment.get_segment_manager ()); std::less<const int> comparator;
MyMap *table = segment.construct<MyMap>("table")(comparator, nodesAlloc);
MyPair myPair1(1, new ShmNode(1,2)); MyPair myPair2(2, new ShmNode(3,4)); MyPair myPair3(3, new ShmNode(5,6));
table->insert(myPair3); table->insert(myPair1); table->insert(myPair2);
return 0; } --------------------------------------------------
Proc#2 -------------------------------------------------- /* same includes */
/* same typedefs */
int main() { boost::shmem::named_shared_object segment; segment.open("/myshmem");
MyMap *table = segment.find<MyMap>("table").first;
std::cout << "table size: " << table->size() << std::endl;
MyMap::iterator iter; for (iter = table->begin(); ; iter++) { if (iter == table->end()) break; std::cout << iter->second->getA() << " " << iter->second->getB() << std::endl; } return 0; } --------------------------------------------------
The problem once again: in proc#2, iter->second->getA() and iter->second->getB() return 0. If I put the same loop into proc#1, the correct values are returned. As a further hint, the size of the map object in proc#2 is returned correctly (=3).
What am I doing wrong?
Regards, Szilard _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Adam Lewis
-
Jan Stetka
-
Pataki Szilard