我正在离散模拟器中编写一个基于代理的小型交互模拟,并开始编写一些如下所示的代码。我之前没有一些事件驱动的编程,但并没有真正观察到这种情况。我想知道以下代码是否会在更新msgRcvd
.
// Following is the event-loop per-se
Controller {
if (...) {
SendMessage(currentTime() + 5, i,j)
SendMessage(currentTime() + 5, i,k)
}
print currentTime(), msgsRcvd
Schedule(currentTime()+1, Controller)
}
// The following function is called when an
// agent receives a message
Receive(Agent agent) {
if (...) {
msgsRcvd++ // <-- this is a global variable
}
}
我的理解是,currentTime() + 5
两个代理同时收到消息,因为这两个事件都在同一逻辑时间发生,所以我应该看到消息数为 2?或者我会看到一些奇怪的竞争条件发生并且值取决于调度程序(即它最终可能打印 1 或 2)?有什么建议么?