如何测试 boost 共享内存对象是否被删除?

发布于 2024-11-03 09:46:06 字数 574 浏览 2 评论 0原文

我在进程之间使用 boost::interprocess::shared_memory_object 。

进程(服务器)创建共享内存对象,其他进程(客户端)打开该共享内存对象。但是,客户端进程如何确定该共享内存对象是否被服务器进程删除?

正如Boost文档所述< /a>,当请求删除共享内存时,直到所有其他引用取消引用它为止,它不会被删除。因此,即使服务器进程(尝试)删除共享内存对象,该共享内存对象也不会被删除,因此其他客户端进程无法知道该对象是否被删除。

由于引用缓存,我需要这种删除检测。我的意思是,客户端进程存储对共享内存的引用,并在需要时重用它。但是,当服务器进程删除共享内存并使用相同名称重新创建共享内存时,我只想让我的客户端进程检测到这一重新创建并更新对新创建的共享内存对象的引用。

I'm using boost::interprocess::shared_memory_object between the processes.

A process (server) creates a shared memory object, and other processes (clients) open that shared memory object. But, how can client processes determine if that shared memory object is removed by server process?

As boost documentation states, when the shared memory is requested to be removed, it won't be deleted until all other references de-refer it. So, even if the server process (tries to) delete the shared memory object, that shared memory object is not deleted, and, so other client processes cannot know that was deleted or not.

I need this kind of delete-detection because of the reference caching. I mean, client processes stores the reference to the shared memory, and reuse it whenever needed. But, when the server process delete the shared memory and recreate one using the same name, I just want my client processes to detect this recreation and update the reference to the newly created shared memory object.

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

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

发布评论

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

评论(2

咽泪装欢 2024-11-10 09:46:06

该命令

ipcs -m

将显示所有现有的共享内存段。到shmid时,您应该能够识别您的细分。还有 nattch 列,显示该段的附加进程数。

The command

ipcs -m

will display all existing shared memory segments. By shmid you should be able to identify your segment. There's also nattch column showing number of attached processes to the segment.

你的往事 2024-11-10 09:46:06

您可以在共享内存对象中包含一个标志,指示您的服务器进程是否仍在使用它。

在服务器进程尝试删除共享内存之前,将此标志设置为 false。当客户端进程发现该标志为 false 时,它​​们可以关闭对该对象的引用。

另外,我不认为服务器进程将被允许重新创建具有相同名称的共享对象,直到它被删除,因为我确信名称必须是唯一的。

You could include a flag in your shared memory object that indicates if your server process is still using it.

Set this flag to false before your server process attempts to delete the shared memory. When the client processes see that the flag is false they can close their references to the object.

Also, I don't think the server process will be permitted to recreate a shared object with the same name until it has been deleted, as I am sure the names must be unique.

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