问题描述
它包含五个部分:
-
SharedEvent是存储在共享内存中的AutoResetEvent的实现.
SharedEvent is an implementation of an AutoResetEvent that is stored in shared memory.
CreatedSharedEvent创建一个分配了SharedEvent的命名共享内存对象.它提供了一个访问器方法,该方法返回对SharedEvent的引用.
CreatedSharedEvent creates a named shared memory object in which a SharedEvent is allocated. It provides an accessor method that returns a reference to the SharedEvent.
OpenedSharedEvent将打开已在其中分配SharedEvent的命名共享内存对象.它还提供了一个访问器方法,该方法返回对SharedEvent的引用.
OpenedSharedEvent opens a named shared memory object in which a SharedEvent has already been allocated. It also provides an accessor method that returns a reference to the SharedEvent.
一个服务器控制台应用程序,它使用CreatedShareEvent创建SharedEvent并每2秒设置一次事件.每次设置事件时都会打印一条消息.
A server console application that creates a SharedEvent using a CreatedShareEvent and sets the event every 2 seconds. It prints a message every time the event is set.
一个控制台应用程序,它使用OpenedShareEvent打开共享事件并循环等待该事件.每当等待调用返回时,它都会打印一条消息.
A console application that opens the shared event using an OpenedShareEvent and waits on the event in a loop. It prints a message every time the wait call returns.
重现该问题:
-
运行服务器.观察每2秒打印一次的消息.
Run the server. Observe the messages printed every 2 seconds.
运行客户端.观察每2秒打印一次的消息.
Run the client. Observe the messages printed every 2 seconds.
关闭客户端.观察服务器停止打印消息.它在interprocess_condition :: notify_one()
Close the client. Observe that the server ceases to print messages. It is blocked in interprocess_condition::notify_one()
推荐答案
问题的原因与此处:
在进程可能崩溃并仍然保持锁定的情况下,不能使用boost_interprocess.
This use of boost interprocess cannot be used in a situation where a process might crash and still be holding the lock.
我将发布一个不同的问题,以查看是否有人发现可以很好地替代condition_variable和interprocess_mutex.
I will post a different question to see if anyone has discovered a good replacement for boosts condition_variable and interprocess_mutex.
这篇关于为什么boost的interprocess_condition死锁在notify_one中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!