提高:: named_mutex:安全地清理时,最后一个进程关闭 [英] boost::named_mutex: Safely cleaning up when last process closes

查看:1303
本文介绍了提高:: named_mutex:安全地清理时,最后一个进程关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资源,我需要保护访问过程中,并在多个进程。我已经通过通过的boost ::进程间创建一个名为互斥管理这样的:named_recursive_mutex ,它的伟大工程

I have a resource which I need to protect access to within a process, and across multiple processes. I've managed this by creating a named mutex via boost::interprocess:named_recursive_mutex, and it works great.

#include <boost/interprocess/sync/named_recursive_mutex.hpp>
boost::interprocess::named_recursive_mutex mut(
    boost::interprocess::open_or_create, "MY_SHARED_MUTEX_123");

然而,这是我的理解,这应该可以通过最终清理删除(),即:

mut.remove("MY_SHARED_MUTEX");

不过,这似乎呼吁彻底揍互斥,而不是检查/递减引用计数,所以我试图找到一种安全的方式发出删除()打电话的时候,我知道没有其他进程正在使用它。我可以创建通过提升共​​享内存块为好,但是这似乎并没有设有共用引用计数。

However, this call appears to completely clobber the mutex, rather than check/decrement a reference count, so I'm trying to find a safe way to issue the remove() call when I know that no other processes are using it. I could create a chunk of shared memory via boost as well, but that does not appear to have a shared reference count either.

我发现了类似的问题对SO ,而是<一个HREF =htt​​p://stackoverflow.com/a/7561805>接受的答案似乎不能满足我的需求,因为它只是指的助推文档,但并没有给出明确的指示,至于何时删除()能安全地发行。

I've found a similar question on SO, but the accepted answer doesn't seem adequate for my needs, as it just refers to the "boost docs", but doesn't give a clear indicator as to when remove() can be safely issued.

如何才能安全地收拾这个命名的互斥体时,我敢肯定的最后一个进程访问它已关闭,或可能崩溃?

How can I safely clean up this named mutex when I'm certain the last process accessing it has closed, or possibly crashed?

感谢您。

推荐答案

不存在任何引用计数支持我的知识。

如果您正在启动或停止例如使用shell脚本的过程,你可以在启动时删除共享内存文件/从脚本阻止。

If you are starting or stopping your processes using for example shell script you could remove the shared memory files at start/stop from the script.

如果你有一个过程,总是第一个开始,那么你可以使用它(在程序停止在最后停止过程或类似),以去除在程序开始共享内存文件。

If you have one process that is always starting first then you could use it to remove shared memory files at program start (or similarly in last stopping process at program stop).

另一种方法是实现参考使用共享内存计算自己 - 最后过程将删除所有共享内存文件。但是,如果进程崩溃的一个,你仍然可以尝试在段错误信号处理程序删除,但是这并不总是工作太这是不行的。

Another approach would be implementing reference counting yourself using shared memory - last process would delete all shared memory files. However this won't work if one of the processes crash, you could still try removing in segfault signal handler but this may not always work too.

更新:作为@sehe提到的boost ::进程间:: shared_ptr的可用于引用计数

Update: As @sehe mentioned boost::interprocess::shared_ptr can be used for reference counting.

这篇关于提高:: named_mutex:安全地清理时,最后一个进程关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆