Reputation: 1467
I am having two different applications developed in C++
. These applications communicate each other via boost::interprocess::shared_memory_object
. Now I require a mutex
variable to sync the operations between the two.. Can somebody provide a minimal example for this. The code of my applications is as follows:
App A:
boost::interprocess::shared_memory_object shm (boost::interprocess::create_only, "MySharedMemory", boost::interprocess::read_write);
shm.truncate (1024);
boost::interprocess::mapped_region region(shm,boost::interprocess::read_write);
std::strcpy(static_cast<char* >(region.get_address()), "WRITE\n");
/*I need to write some data to shared memory here. But line by line. i.e., I write first line. App B reads it. Then I write second line. For this i require mutex*/
App B:
boost::interprocess::shared_memory_object shm (boost::interprocess::open_only, "MySharedMemory", boost::interprocess::read_write);
boost::interprocess::mapped_region region (shm, boost::interprocess::read_write);
char *str1 = static_cast<char*> (region.get_address());
char subbuff[5];
memcpy( subbuff, str1, 5 );
subbuff[5] = '\0';
while(strcmp(subbuff,"WRITE")!=0)
{
str1 = static_cast<char*> (region.get_address());
memcpy( subbuff, str1, 5 );
subbuff[5] = '\0';
}
/*Here I want to read the data written by App A line by line*/
Upvotes: 0
Views: 200