3

I'm using boost::interprocess::named_upgradable_mutex to synchronize a few processes.

I'm using boost::interprocess::sharable_lock and boost::interprocess::scoped_lock to lock the mutex.

When testing the synchronization, it looks fine as long as the processes are working, and are closed normally.

But, I've notices that if a process is killed (via TaskManager for example) while holding the mutex, the mutex remains locked.

Any idea how i can handle process failures ?

I've thought about using timed_lock() just in case... any other ideas?

4

2 回答 2

1

您正在解决症状而不是问题。互斥体的目的是允许进程或线程将共享数据置于不一致的状态。如果进程在持有互斥锁时死亡,则共享数据仍处于不一致状态。问题是如何将共享数据返回到一致状态,而不是如何解锁互斥锁。

当您将共享数据返回到一致状态时,在您返回到一致状态的数据中包含互斥锁或锁定。最简单的方法是删除现有锁并创建一个新锁。您可能必须对共享数据执行相同的操作。

如果你真的需要这样做,我建议你可能没有使用正确的工具来完成这项工作。

于 2011-12-14T09:27:21.683 回答
0

如果您出于某种原因杀死了您的应用程序,您可以通过从 Windows 注销或执行命令 mutex.unlock() 来解锁此锁;

于 2012-05-12T20:20:49.933 回答