【问题标题】:Getting offset_ptr of object in shared memory获取共享内存中对象的 offset_ptr
【发布时间】:2010-10-11 14:50:22
【问题描述】:

如何访问在共享内存中创建的对象的 offset_ptr?

segment = new managed_shared_memory(create_only, "MySharedMemory", segmentSize);  
line = segment->construct<Line>("Line1")("line");

我想访问 Line 对象的 offset_ptr..

【问题讨论】:

    标签: boost shared-memory


    【解决方案1】:

    首先,你需要用你的 Line 类型声明偏移指针:

    boost::interprocess::offset_ptr<Line> offset_p;
    

    其次,你给它赋值:

    offset_p = line;
    

    现在,offset_p 保存了从 line 到自身的偏移量,因此您可以获取指向 line 的指针并在任何进程中使用它,如下所示:

    draw_line( offset_p.get() );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多