0

I search to shared a struct between two process. But I have not success.Can you help to understand ?

Here is my code for first process :

#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include "semaphore_shared_data.hpp"

using namespace boost::interprocess;


int main(){

shared_memory_object shm(open_or_create, "shared_memory", read_write);
shm.truncate(sizeof(shared_memory_buffer));
mapped_region region(shm,read_write);

void *addr = region.get_address();
shared_memory_buffer *data = new (addr) shared_memory_buffer;
while(true){

   data->writer.wait();
   std::cout << "Process 1 read: " << data->Variable.Type << ":" <<data->Variable.Value << std::endl;

  data->Variable.Type = "ACK";
  data->Variable.Value = 1;
  sleep(1);
  data->reader.post();
}
return 0;
}

Here is my code for second process :

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <iostream>
#include <math.h>
#include <string>
#include "semaphore_shared_data.hpp"

using namespace boost::interprocess;


int main ()
{

 shared_memory_object shm(open_only, "shared_memory",read_write);
 mapped_region region(shm,read_write);
 void *addr = region.get_address();

 shared_memory_buffer *data = static_cast<shared_memory_buffer*>(addr);
 while(true)
 {

   data->reader.wait();
   std::cout << "Process 2 read: " << data->Variable.Type << ":" <<    data->Variable.Value << std::endl;
   data->Variable.Type = "ACK";
   data->Variable.Value = 2;
   data->writer.post();
 }
 return 0;
}

here is my code for shared_memory_buffer (semaphore_shared_data.hpp)

#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
using namespace boost::interprocess;

typedef allocator<char, managed_shared_memory::segment_manager>  CharAllocator;
typedef basic_string<char, std::char_traits<char>, CharAllocator>shared_string;

struct shared_Struct
{
  shared_Struct():Type("ACK"), Value(0){}
  shared_string Type;
  float Value;

};

struct shared_memory_buffer 
{

   shared_memory_buffer(): writer(1), reader(0), Variable(){}
   interprocess_semaphore writer, reader;

   shared_Struct Variable;

};

I have this errors :

semaphore_shared_data.hpp: In constructor ‘boost::container::basic_string<CharT, Traits, Allocator>::basic_string(const CharT*, const allocator_type&) [with CharT = char; Traits = std::char_traits<char>; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; boost::container::basic_string<CharT, Traits, Allocator>::allocator_type = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’:

semaphore_shared_data.hpp:16:38: error: no matching function for call to ‘boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()’ shared_Struct():Type("ACK"), Value(0){}
1
  • How about showing us the code where the error is, and formatting all this so it is readable? Commented Sep 30, 2014 at 14:33

1 Answer 1

1

I think my mistake is due this declaration :

typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator; 
typedef basic_string<char, std::char_traits<char>, CharAllocator>shared_string;

gcc indicates :

shared_Struct():Type("ACK"), Value(0){}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.