Boost logo

Boost Users :

Subject: [Boost-users] [Interprocess] managed_shared_memory between 32 & 64bit
From: leunm (leunm_at_[hidden])
Date: 2013-02-17 13:43:18


Hi, I am new to Boost and am looking for a way to pass xyz coordinates from a
32-bit application to a 64-bit one. I see two recent posts on this same
topic, but the explanation, as well as the Boost documentation (on
offset_ptr) is a bit sparse. Below is a simple example I modified from the
Boost documentations
<http://www.boost.org/doc/libs/1_53_0/doc/html/interprocess/quick_guide.html#interprocess.quick_guide.qg_interprocess_container>
, which works for either 32-bit or 64-bit but not when the parent is 32-bit
and child is 64-bit, and vice versa. Can someone tell me what I need to do
enable this function?

Thank you in advance,
Mike

-----------------------------------------------------------------

#define BOOST_DATE_TIME_NO_LIB

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <cstdlib> //std::system
#include <conio.h>
#include <iostream>
#include <windows.h>

using namespace boost::interprocess;

//Define an STL compatible allocator of ints that allocates from the
managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<float, managed_shared_memory::segment_manager>
ShmemAllocator;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef vector<float, ShmemAllocator> MyVector;

//Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char *argv[])
{
        if(argc == 1){ //Parent process
                //Remove shared memory on construction and destruction
                struct shm_remove
                {
                        shm_remove() { shared_memory_object::remove("MySharedMemory"); }
                        ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
                } remover;

                //Create a new segment with given name and size
                managed_shared_memory segment(create_only, "MySharedMemory", 65536);

                //Initialize shared memory STL-compatible allocator
                const ShmemAllocator alloc_inst (segment.get_segment_manager());

                //Construct a vector named "MyVector" in shared memory with argument
alloc_inst
                MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);

                for(int i = 0; i < 100; ++i) //Insert data in the vector
                        myvector->push_back(0.0);

                // Loop
                bool exitFlag = false;
                while (1) {

                        // Store 3 values
                        myvector->at(0) = rand() % 100;
                        myvector->at(1) = rand() % 100;
                        myvector->at(2) = rand() % 100;

                        std::cout << "Parent: " << myvector->at(0) << ", " << myvector->at(1) <<
", " << myvector->at(2) << std::endl;

                        if (kbhit())
                        {
                                switch (getch())
                                {
                                default:
                                        exitFlag = true;
                                        break;
                                }
                                if (exitFlag)
                                        break;
                        }

                        Sleep(500);
                }

                //When done, destroy the vector from the segment
                segment.destroy<MyVector>("MyVector");

        } else { //Child process

                //Open the managed segment
                managed_shared_memory segment(open_only, "MySharedMemory");

                //Find the vector using the c-string name
                MyVector *myvector = segment.find<MyVector>("MyVector").first;

                //Loop
                bool exitFlag = false;
                while (1) {
                        try {
                                std::cout << "Child: " << myvector->at(0) << ", " << myvector->at(1) <<
", " << myvector->at(2) << std::endl;
                        } catch (...) {
                                std::cout << "Error accessing vector, exiting..." << std::endl;
                                exitFlag = true;
                        }

                        if (kbhit())
                        {
                                switch (getch())
                                {
                                default:
                                        exitFlag = true;
                                        break;
                                }
                        }

                        if (exitFlag)
                                break;

                        Sleep(500);
                }
        }

        return 0;
};

--
View this message in context: http://boost.2283326.n4.nabble.com/Interprocess-managed-shared-memory-between-32-64bit-tp4642955.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net