Почему managed_shared_memory :: find () не возвращает тот же адрес, что и managed_shared_memory :: construct ()? - PullRequest
0 голосов
/ 05 августа 2020

У меня проблема, когда я использую Boost Interprocess для записи некоторых значений в общую память с помощью managed_shared_memory :: construct (), а затем в другом процессе пытается прочитать эти значения с помощью managed_shared_memory :: find (), но этого не происходит обратно с тем же адресом, который был создан с помощью construct ().

Я взял образец кода из документации Boost Interprocess для Создание именованных объектов общей памяти и разделил его на две разные программы и одну и ту же что-то происходит.
interprocess_write. cpp

#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> //std::system
#include <cstddef>
#include <cassert>
#include <iostream>
#include <utility>

int main(int argc, char *argv[])
{
   using namespace boost::interprocess;
   typedef std::pair<double, int> MyType;

   if(argc == 1){  //Parent process
       shared_memory_object::remove ("MySharedMemory");

      //Construct managed shared memory
      managed_shared_memory segment(create_only, "MySharedMemory", 65536);

      //Create an object of MyType initialized to {0.0, 0}
      MyType *instance = segment.construct<MyType>
         ("MyType instance")  //name of the object
         (0.0, 0);            //ctor first argument

      //Create an array of 10 elements of MyType initialized to {0.0, 0}
      MyType *array = segment.construct<MyType>
         ("MyType array")     //name of the object
         [10]                 //number of elements
         (0.0, 0);            //Same two ctor arguments for all objects

      //Create an array of 3 elements of MyType initializing each one
      //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
      float float_initializer[3] = { 0.0, 1.0, 2.0 };
      int   int_initializer[3]   = { 0, 1, 2 };

      MyType *array_it = segment.construct_it<MyType>
         ("MyType array from it")   //name of the object
         [3]                        //number of elements
         ( &float_initializer[0]    //Iterator for the 1st ctor argument
         , &int_initializer[0]);    //Iterator for the 2nd ctor argument

      std::cout << array << ":" << instance << ":" << array_it << std::endl;

      //Check child has destroyed all objects
      if(segment.find<MyType>("MyType array").first ||
         segment.find<MyType>("MyType instance").first ||
         segment.find<MyType>("MyType array from it").first)
         return 1;
   }
   return 0;
}

interprocess_read. cpp

#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> //std::system
#include <cstddef>
#include <cassert>
#include <iostream>
#include <utility>


int main (int argc, char **argv)
{
       using namespace boost::interprocess;
       typedef std::pair<double, int> MyType;

  //Open managed shared memory
  managed_shared_memory segment(open_only, "MySharedMemory");

  std::pair<MyType*, managed_shared_memory::size_type> res;

  //Find the array
  res = segment.find<MyType> ("MyType array");
  //Length should be 10
  if(res.second != 10) return 1;

  std::cout << res.first << ":";

  //Find the object
  res = segment.find<MyType> ("MyType instance");
  //Length should be 1
  if(res.second != 1) return 1;

  std::cout << res.first << ":";

  //Find the array constructed from iterators
  res = segment.find<MyType> ("MyType array from it");
  //Length should be 3
  if(res.second != 3) return 1;

  std::cout << res.first << std::endl;

  //We're done, delete all the objects
  segment.destroy<MyType>("MyType array");
  segment.destroy<MyType>("MyType instance");
  segment.destroy<MyType>("MyType array from it");
}

Когда я запускал программа interprocess_write я получил следующий результат:

0x1260128: 0x12600d8: 0x1260208

Но когда я запускаю программу interprocess_read, я получаю:

0x2ad0128: 0x2ad00d8: 0x2ad0208

Что-то не хватает, что нужно сделать в interprocess_read, чтобы заставить его получать правильные адреса или т общей памяти?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...