开发者

How can I test if the boost shared memory object is removed or not?

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 ot开发者_如何学Cher 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜