C++ BOOST:Windows 共享内存 get_size() 返回零

发布于 2024-09-11 15:30:06 字数 446 浏览 9 评论 0原文

以下内容来自 BOOST 官方文档。 为什么我在调用 region.get_size() 时得到的大小总是为零?我做错了什么?

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_write);
  cout << "SIZE IS " << region.get_size() << endl;

return 0;
}

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_write);
  cout << "SIZE IS " << region.get_size() << endl;

return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-09-18 15:30:06

我想我得到了答案:
来自增强文档:

本机 Windows 共享内存也有
另一个限制:进程可以打开
并映射整个共享内存
由另一个进程创建,但它
不知道它的尺寸是多少
记忆。此限制是由
Windows API,因此用户必须
以某种方式传输的大小
进程打开的段
段。

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.

鹤舞 2024-09-18 15:30:06

boost::interprocess您正在使用的 mapped_region 构造函数的文档说:

创建映射的映射区域
内存“映射”,从偏移量开始
“offset”,并且映射的大小将
是“尺寸”
。可以打开映射
对于只读“read_only”或
读写“read_write。

因此提供非零大小,一切都会按预期工作:
mapped_region 区域(shm, read_write, 0, 1000);

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文