So many options here: since you might end-up supporting complex interactions with multiple threads, you might want to consider using a "virtual clock" with a message passing "bus". This way, you'll have more time to focus on the core functionality instead of debugging the synchonization logic...
Using this technique, you can build a state-machine (see here) per "actor" thread (worst case) and worry less about mutexes/conditions. Once you've got this base, you'll be able to handle cases that show up mid-stream (e.g. "I forgot about this detail... no worries, just add a state here... don't have to re-shuffle my mutexes).
Also, since doing simulation is all about "virtual time" (since you can't run real-time !), then having a base architecture based on a "virtual clock" abstracts the problem to an appropriate level.
您可以有一个时钟线程加 N 个处理器线程。时钟线程可以显式调用每个处理器线程以执行 1 个处理步骤。每个处理器线程都会从时钟线程调用doStep();它执行一步,然后将控制权返回给时钟线程。
您还可以随机化调用处理器线程执行步骤的顺序,以便更有可能捕获客户端代码中的错误。
You could have a clock thread plus N processor threads. The clock thread can explicitly make calls to each processor thread to make 1 processing step. Each processor thread gets a call from the clock thread to doStep(); it makes one step, then returns control back to the clock thread.
You could also randomize the order in which the processor threads are called to do steps so that you're more likely to catch bugs in your client code.
发布评论
评论(2)
这里有很多选择:由于您最终可能需要支持与多个线程的复杂交互,因此您可能需要考虑使用“虚拟时钟”以及传递“总线”的消息。这样,您将有更多时间专注于核心功能,而不是调试同步逻辑...
使用这种技术,您可以构建状态机(请参阅此处)每个“演员”线程(最坏的情况),不用担心互斥体/条件。一旦你有了这个基础,你就能够处理中流出现的情况(例如“我忘记了这个细节......不用担心,只需在此处添加一个状态......不必重新-洗牌我的互斥体)。
此外,由于进行模拟都是关于“虚拟时间”(因为您无法实时运行!),因此拥有基于“虚拟时钟”的基础架构可以将问题抽象到适当的级别。
So many options here: since you might end-up supporting complex interactions with multiple threads, you might want to consider using a "virtual clock" with a message passing "bus". This way, you'll have more time to focus on the core functionality instead of debugging the synchonization logic...
Using this technique, you can build a state-machine (see here) per "actor" thread (worst case) and worry less about mutexes/conditions. Once you've got this base, you'll be able to handle cases that show up mid-stream (e.g. "I forgot about this detail... no worries, just add a state here... don't have to re-shuffle my mutexes).
Also, since doing simulation is all about "virtual time" (since you can't run real-time !), then having a base architecture based on a "virtual clock" abstracts the problem to an appropriate level.
您可以有一个时钟线程加 N 个处理器线程。时钟线程可以显式调用每个处理器线程以执行 1 个处理步骤。每个处理器线程都会从时钟线程调用
doStep()
;它执行一步,然后将控制权返回给时钟线程。您还可以随机化调用处理器线程执行步骤的顺序,以便更有可能捕获客户端代码中的错误。
You could have a clock thread plus N processor threads. The clock thread can explicitly make calls to each processor thread to make 1 processing step. Each processor thread gets a call from the clock thread to
doStep()
; it makes one step, then returns control back to the clock thread.You could also randomize the order in which the processor threads are called to do steps so that you're more likely to catch bugs in your client code.