簡體   English   中英

如何在 C++ 中跨多個進程使用共享向量

[英]How to use share vectors across multiple processes in C++

我在進程之間共享向量時遇到問題。 我可以共享向量,甚至可以從不同的進程中獲取向量的大小,但是當我在 function 使用時,程序就會崩潰。

    struct B
   {
      std::vector<int> vec;
   };

    int main(int cArgs, char* ppszArgs[])
   {
       if (cArgs == 1) {  //Parent process
          //Remove shared memory on construction and destruction
       struct shm_remove
        {
        shm_remove() { shared_memory_object::remove("MySharedMemory"); }
        ~shm_remove() { shared_memory_object::remove("MySharedMemory"); }
       } remover;

    //Create a shared memory object.
    shared_memory_object shm(create_only, "MySharedMemory", read_write);

    //Set size
    shm.truncate(1000);

    //Map the whole shared memory in this process
    mapped_region region(shm, read_write);

    //Write all the memory to 1
    B* test = new B();


    CopyMemory(region.get_address(), test, sizeof(B));


    parentProcess(); -> this method just starts the child process

    int index = 1;
    while (true)
    {
        if(index < 2)
        {
            ((B*)region.get_address())->vec.push_back(index);
        }
        ++index;
    }

}
else
{
    //Open already created shared memory object.
    shared_memory_object shm(open_only, "MySharedMemory", read_only);

    //Map the whole shared memory in this process
    mapped_region region(shm, read_only);

    //Check that memory was initialized to 1
    HANDLE mem = region.get_address();


    while(true)
    {
        std::cout << ((B*)mem)->vec.at(0) << std::endl; -> if for example i put 
        lista.size(), then i will get the number of items in vector.
    }

}

}

我的問題是甚至可以從子進程訪問矢量元素嗎?

是的,你絕對可以做到這一點。 您需要使用 boost 進程間庫。 您需要 (1) 一個互斥鎖來仲裁對向量的訪問,以及 (2) 您需要能夠在共享 memory 中分配向量元素。 您應該能夠在共享 memory 的一段中執行此操作。 但是我無法在短時間內讓它工作。 這是使用兩個共享 memory 段的解決方案,一個用於互斥體,一個用於向量。

#include <iostream>
#include <string>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp> 

using namespace boost::interprocess;

typedef allocator<int, managed_shared_memory::segment_manager>
ShmemAllocator;

typedef std::vector<int, ShmemAllocator> MyVector;


struct B
{
    boost::interprocess::interprocess_mutex mutex;
};

int main(int cArgs, char* ppszArgs[])
{

    if (cArgs== 1) {  //Parent process
        std::cout << "In parent" << std::endl;

       //Remove shared memory on construction and destruction
        struct shm_remove
        {
            shm_remove() { shared_memory_object::remove("MutexMemory"); }
            ~shm_remove() { shared_memory_object::remove("MutexMemory"); }
        } remover;
        shared_memory_object::remove("VectorMemory");
        managed_shared_memory segment
        (create_only
            , "VectorMemory" //segment name
            , 65536);          //segment size in bytes


        const ShmemAllocator alloc_inst(segment.get_segment_manager());

        shared_memory_object shm(create_only, "MutexMemory", read_write);


        MyVector* myvector =
            segment.construct<MyVector>("MyVector") //object name
            (alloc_inst);//first ctor parameter

        shm.truncate(1000);

        mapped_region region(shm, read_write);
        void* addr = region.get_address();
        B* test = new (addr) B();



        //        parentProcess(); -> this method just starts the child process

        int index = 1;
        while (true)
        {
            if (index < 10000)
            {
                scoped_lock<interprocess_mutex> lock(test->mutex);
                myvector->push_back(index);
            }
            Sleep(1000);
            ++index;
        }

    }
    else
    {
        std::cout << "In child" << std::endl;
        //Open already created shared memory object.
        shared_memory_object shm(open_only, "MutexMemory", read_write);

        //Map the whole shared memory in this process
        mapped_region region(shm, read_write);
        B* mem = (B*)region.get_address();
        managed_shared_memory segment
        (open_only
            , "VectorMemory");  //segment name

        MyVector* myvector = segment.find<MyVector>("MyVector").first;

        while (true)
        {
            if(1)
            {
                scoped_lock<interprocess_mutex> lock(mem->mutex);
                std::cout << (myvector->size() == 0 ? -1 : (*myvector)[myvector->size() - 1]) << std::endl;
            }
            Sleep(1000);

        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM