views:

55

answers:

1

The main application needs to update the shared memory at a fast frequency.

And several consuming applications need to read from the shared memory to update the streamed data.

main and consuming applications are different processes.

How to implement this with Boost.Interprocess ?

+1  A: 

producer:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/thread.hpp>
#include <iostream>

struct shared_data_t {
   boost::uint32_t data;
   boost::interprocess::interprocess_mutex mutex;
};

/***************************************************************************/

/* producer */
int main(int argc, char** argv) {
   const char* shname = "_unique_object_name_";
   boost::shared_ptr<const char> remover(
      shname,
      boost::interprocess::shared_memory_object::remove
   );
   try {
      boost::interprocess::shared_memory_object shared_object(
         boost::interprocess::create_only,
         shname,
         boost::interprocess::read_write
      );
      shared_object.truncate(sizeof(shared_data_t));
      boost::interprocess::mapped_region region(
         shared_object,
         boost::interprocess::read_write
      );
      shared_data_t* data = new(region.get_address())shared_data_t;
      assert(data);
      const boost::uint32_t count = 0x1000;
      for ( boost::uint32_t idx = 0; idx < count; ++idx ) {
         {  boost::interprocess::scoped_lock<
               boost::interprocess::interprocess_mutex
            > lock(data->mutex);
            data->data = idx;
         }
         boost::this_thread::sleep(boost::posix_time::seconds(1));
      }
   } catch(boost::interprocess::interprocess_exception &e){
      std::cout << e.what() << std::endl;
      return 1;
   }

   return 0;
}

consumer:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/thread.hpp>

struct shared_data_t {
   boost::uint32_t data;
   boost::interprocess::interprocess_mutex mutex;
};

/***************************************************************************/

/* consumer */
int main(int argc, char** argv) {
   try {
      boost::interprocess::shared_memory_object shared_object(
         boost::interprocess::open_only,
         "_unique_object_name_",
         boost::interprocess::read_only
      );
      shared_object.truncate(sizeof(shared_data_t));
      boost::interprocess::mapped_region region(
         shared_object,
         boost::interprocess::read_only
      );
      shared_data_t* data = new(region.get_address())shared_data_t;
      assert(data);
      while ( true ) {
         {  boost::interprocess::scoped_lock<
               boost::interprocess::interprocess_mutex
            > lock(data->mutex);
            std::cout << "ping: " << data->data << std::endl;
         }
         boost::this_thread::sleep(boost::posix_time::milliseconds(100));
      }
   } catch(boost::interprocess::interprocess_exception &e){
      std::cout << e.what() << std::endl;
      return 1;
   }
   return 0;
}

man: http://www.boost.org/doc/libs/1_43_0/doc/html/interprocess/synchronization_mechanisms.html

niXman