Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to share a dynamic array of characters between two processes using Boost Shared Memory. I use the following two pieces of code to do that:

Producer Process:

C++
char *data;
unsigned int share_length;
unsigned int offset;
std::string data_to_share(data + offset, data + offset + share_length);

boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::create_only,
                                            "SomeName", 1024*1024);
typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> my_string;

managed_shm.construct<my_string>("DataToShare")(data_to_share.c_str(), managed_shm.get_segment_manager());


Consumer Process:

C++
boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_read_only, "SomeName");
typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> my_string;

std::pair<my_string *, size_t > data = managed_shm.find<my_string>("DataToShare");

cout << "Data Size=" << data.first->length() << endl;


The problem is when I try to print the length of the DataToShare in the Consumer Process it is wrong, so what is the problem in my code?
Is it in the Producer Part since I am doing data_to_share.c_str() which is a std::string type instead of of my_string type?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900