如何使用Boost.Interprocess将数据流式传输到其他应用程序?

wam*_*amp 7 c++ boost communication

main应用程序需要以快速的频率来更新所述共享存储器.

并且有几个consuming应用程序需要从共享内存中读取以更新流数据.

mainconsuming应用程序是不同的过程.

如何使用Boost.Interprocess实现这一点?

niX*_*man 5

制片人:

#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;
}
Run Code Online (Sandbox Code Playgroud)

消费者:

#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;
}
Run Code Online (Sandbox Code Playgroud)

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