开发者

C++ BOOST: windows shared memory get_size() returns zero

The below is from the official BOOST docs. Why do I always get size of zero when calling region.get_size() ? What am I doing wrong?

int main(int argc, char *argv[])
{

  //Create a native windows shared memory object.
  windows_shared_memory shm (create_only, "MySharedMemory", read_write, 1000);

  //Map the whole shared memory in this process
  mapped_region region(shm, read_writ开发者_运维问答e);
  cout << "SIZE IS " << region.get_size() << endl;

return 0;
}


I think I got the answer: From boost docs:

Native windows shared memory has also another limitation: a process can open and map the whole shared memory created by another process but it can't know which is the size of that memory. This limitation is imposed by the Windows API so the user must somehow transmit the size of the segment to processes opening the segment.


In the boost::interprocess documentation for the mapped_region constructor you're using it says:

Creates a mapping region of the mapped memory "mapping", starting in offset "offset", and the mapping's size will be "size". The mapping can be opened for read-only "read_only" or read-write "read_write.

So provide a non-zero size and everything will work as expected: mapped_region region(shm, read_write, 0, 1000);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜