【发布时间】:2011-10-04 22:29:30
【问题描述】:
我正在离散模拟器中编写一个基于代理的小型交互模拟,并开始编写一些如下所示的代码。我之前没有一些事件驱动的编程,但并没有真正观察到这种情况。我想知道在更新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)?有什么建议吗?
【问题讨论】:
标签: c++ events simulation event-driven ns-3