我正在尝试处理重复的事件(MIDI 音符开和音符关信号一式三份)。似乎使用代理和锁定工作,除非事件(几乎)同时发生。
这是一个例子:
(def test-val (agent 0))
(def allow-update (agent true))
(defn increase-val []
(locking allow-update
(when @allow-update
(send allow-update (fn [_ x] x) false)
(send test-val + 1)))
(print @test-val))
(defn run-futures [delay-time]
(send allow-update (fn [_ x] x) true)
(send test-val * 0)
(dotimes [_ 20]
(Thread/sleep delay-time)
(future (increase-val))))
如果我在对 increase-val 的调用之间稍有延迟对其进行测试:
(run-futures 2)
;outputs 0111111111111111111 every time, as expected
但是,如果我让所有对 increase-val 的调用同时发生:
(run-futures 0)
;001222222222222222
(run-futures 0)
;000000145555555555
(run-futures 0)
;000000013677777777
似乎锁没有时间打开,因此代理增加了多个期货。
我希望我在这里遗漏了一些东西,可以让我确保我不会对重复的同时发生的事件采取行动。
谢谢!