事件驱动的有限状态机 +线程:如何?
我想用 C 语言建模一个事件驱动的有限状态机,如下所示: http://en.wikipedia.org/wiki/Event-driven_finite_state_machine
但我也会就像要在各个线程中处理的“外部”事件一样。
我可以在某处找到这样的代码吗?或者建议?
I would like to model an event-driven finite state machine in C as proposed here :
http://en.wikipedia.org/wiki/Event-driven_finite_state_machine
But I would also like the 'external' events to be handled in various threads.
Can I find such a code somewhere ? Or advices ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
消息队列是解决问题的一种方法。
如果您想向状态机提供来自其他线程的外部事件,它们可以将这些事件写入将由您的状态机读取的消息队列中。
如果您希望其他线程触发状态机中的操作,它可以写入各种消息队列,每个消息队列都与将从其 MQ 读取的线程关联。
一个缺点是事件按时间顺序排序。如果您的状态机不想处理刚刚从队列中读取的事件,您必须决定如何处理该事件:销毁它,将其放回队列,记住它以供将来使用......
Message queues are a way to solve your problem.
If you want to feed your state machine with external events from other threads, they can write these events in a message queue that will be read by your state machine.
If you want that other threads trigger on actions from your state machine, it can write to various message queues each associated to a thread that will read from its MQ.
One drawback is that events get sorted in chronological order. If your state machine is not in the mood of handling the event it just read from the queue, you have to decide what to do with this event: destroy it, put it back to the queue, remember it for future use...
也许量子框架就是您正在寻找的?请参阅 http://state-machine.com/ 了解更多信息。有适用于许多微控制器以及 Linux 和 Windows 的端口。
Maybe the Quantum Framework is what you are looking for? See http://state-machine.com/ for further information. There are ports for many microcontrollers as well as for linux and windows.