【问题标题】:boost::interprocess - allocate_aligned in shared memory?boost::interprocess - 在共享内存中分配对齐?
【发布时间】:2012-11-05 02:22:37
【问题描述】:

如果我使用allocate_aligned 在一块共享内存中分配对齐的内存块,那么我如何在另一个进程中识别同一块?例如

managed_shared_memory managed_shm(open_or_create, "SharedMemory", 65536);
void *ptr = managed_shm.allocate_aligned(256, 16);

我如何从另一个进程中找到ptr

对于非对齐分配,我只使用find_or_construct,然后显然有一个与分配相关联的名称,这使得可以从另一个进程中找到分配。但是,似乎没有任何方法可以使用 find_or_construct 进行对齐分配,显然我必须错过一些关于如何识别匿名分配的基本要点。

【问题讨论】:

    标签: c++ boost shared-memory memory-alignment boost-interprocess


    【解决方案1】:

    来自下面的docs 的示例。这适用于指向内存返回的 allocate_aligned 方法以及 vanilla allocate 方法的指针。

    //Process A obtains the offset of the address
    managed_shared_memory::handle handle = 
       segment.get_handle_from_address(processA_address);
    
    //Process A sends this address using any mechanism to process B
    
    //Process B obtains the handle and transforms it again to an address
    managed_shared_memory::handle handle = ...
    void * processB_address = segment.get_address_from_handle(handle)
    

    【讨论】:

    • 啊,谢谢 - 我没有想到将偏移量从一个进程发送到另一个进程 - 我猜句柄甚至可以进入共享内存中的命名分配。
    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2012-11-26
    • 2014-11-30
    • 2012-09-06
    相关资源
    最近更新 更多