- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想从 CreatMutex
切换到 boost::interprocess::named_mutex
以将我的应用程序限制为单个实例。当应用程序正常运行和结束时,这两种方法都有效。但是,当应用程序崩溃并使用 boost::interprocess::named_mutex
时,锁不会被释放。我可以通过使用两个 name_mutex 来解决这个问题,但我真的不明白这个问题。
为什么 boost::interprocess::named_mutex
的锁在应用程序崩溃时没有释放,但它是通过 CreatMutex
释放的?有什么区别?
boost::interprocess::named_mutex mutex(boost::interprocess::open_or_create, "my_mutex");
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(mutex, boost::interprocess::try_to_lock);
if(!lock) {
return 1; //exit
}
//application may crash here.
boost::interprocess::named_mutex::remove("my_mutex");
return 1; //exit
最佳答案
警告:我没有花太多时间在 boost::interprocess
上,所以这些信息只是来自对源代码的快速检查。也就是说,我经常使用 Windows 同步 API,所以这里...
这两种进程间同步方法的主要区别在于对象在系统中的存在方式。
有了 boost::interprocess::named_mutex
以及系统特定的互斥锁,同步对象看起来就像是作为系统文件创建的。该文件的位置基于注册表条目(见注释 1)(至少在 Boost 1.54.0 中是这样)...它很可能位于 Common Application Data 文件夹下(见注释 2)。当应用程序崩溃时,在您的情况下,该文件不会被删除。我不确定这是否是设计使然...但是在应用程序崩溃的情况下,最好不要弄乱文件系统,以防万一。
相反,当您使用 CreateMutex
时,会在内核模式下创建一个对象,对于命名的互斥锁,多个应用程序可以访问该对象。您可以通过在创建互斥体时指定名称来获得该互斥体的句柄,而当您对其调用 CloseHandle
时,您将失去该句柄。当没有更多句柄引用它时,互斥对象将被销毁。
其中的重要部分在 documentation 中:
The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
这基本上意味着 Windows 将在您的应用程序之后进行清理。
请注意,如果您不执行 ReleaseMutex
,并且您的应用程序在它终止时拥有该互斥锁,那么等待线程或进程可能/很可能会看到该互斥锁已被放弃(WaitForSingleObject
返回 WAIT_ABANDONED
),并会获得所有权。
对于没有提供解决方案,我深表歉意,但我希望它能回答您关于为什么这两个系统的行为不同的问题。
顺便说一句,使用注册表项获取此信息很糟糕 - 使用 SHGetKnownFolderPath
会更安全,也更不会过时。但我离题了。
根据您的操作系统版本,这可能是 %ALLUSERSPROFILE%\Application Data\boost.interprocess
或 ProgramData\boost.interprocess
,或者完全是其他地方.
关于c++ - boost::interprocess::named_mutex 与 CreateMutex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20379817/
我想我一定是从名称 boost::interprocess 中假设了一些不正确的东西。文档重复 named_mutex 是全局的 here . 虽然我无法让它工作。同一个可执行文件的两个拷贝应该同时运
我使用boost::interprocess在共享内存中创建了boost::multi_index数据结构。有许多客户端进程将访问此数据结构。访问时,我将锁定数据结构。我遇到的问题是,一旦客户端进程正
我在跨进程共享名为 mutex 的 boost 时遇到问题,这些进程由不同的用户创建。第一个进程由服务创建,它以 LocalSystem 身份登录。第二个进程是自己创建的(正常执行进程即可)。 两个进
我有一个资源,我需要在一个进程内和多个进程之间保护对它的访问。我通过 boost::interprocess:named_recursive_mutex 创建一个命名的互斥锁来管理这个,它工作得很好。
我有一个应用程序实现了 boost named_mutex 以锁定 C++ 项目 (Visual Studio) 中的多个模块。我需要不惜一切代价删除所有 boost 依赖项。 还有其他方法可以实现吗
我想使用boost::interprocess 来执行跨多个进程的同步;具体来说,我想在 Linux 操作系统下使用 boost::interprocessnamed_mutex(我使用的是 g++)
我有一个类被设计为一些 boost 共享内存的包装器。当我尝试将 named_mutex 作为类成员并在构造函数中对其进行初始化时,出现编译错误。相关代码如下: template class S
我正在使用 boost 进程间共享内存,但是当我尝试使用 boost::interprocess::scoped_lock 获取 boost::interprocess:named_mutex 时很少
我遇到了一个问题,我需要一个 named_mutex 用于类中的 managed_shared_memory 成员,并收到“无法访问类 boost::interprocess::named_mutex
我做了下面的程序,但是最后还是没有删除named_mutex,打印出“Mutex delete failure”的结果 void IPC::testNamedMutex() { named_mutex
我有一个可以由多个线程创建的类。但是有一个函数需要保护代码,所以我决定使用 boost 进程间互斥体。每个类在其构造函数中创建或打开相同的 Mutex: MyClass::MyClass() {
我想从 CreatMutex 切换到 boost::interprocess::named_mutex 以将我的应用程序限制为单个实例。当应用程序正常运行和结束时,这两种方法都有效。但是,当应用程序崩
我有几个进程,但当时应该只有一个在运行。这意味着假设 Process1 正在运行,如果 Process2 启动,则 Process2 应该等到 Process1做完了。我正在考虑为此使用如下代码的bo
我是一名优秀的程序员,十分优秀!