JBotSim icon indicating copy to clipboard operation
JBotSim copied to clipboard

DefaultClock volatile setTimeUnit

Open remikey opened this issue 5 years ago • 3 comments

@xhanin has kindly brought to my attention the fact that the the current implementation of the DefaultClock (#44) is not perfect. Indeed, the delay field could benefit from the same volatile protection as it has been done for the running field (#20).

Although very unlikely, in the JBotSim use-case described thereafter, a modification made with setTimeUnit() could not be taken into account. Scenario:

  • someone actually wants to modify the delay of the DefaultClock while the clock is running; it is generally intended to be used without user interactions.
  • we get very unlucky with CPU caching.

Aside from this unlikely use-case, if someone were to take the DefaultClock as an inspiration for any implementation involving threads, locks and conditions, this might result in spreading an incorrect implementation. If not to be fixed, I think that at least adding a comment to the delay field would be nice.

remikey avatar Feb 07 '20 09:02 remikey

Both seem equally fine. Please do as you see fit.

acasteigts avatar Feb 18 '20 16:02 acasteigts

In branch fix/98-defaultclock-volatile-timeunit:

  • renamed delay field into timeUnit to fit its setter/getter

  • turned timeUnit into volatile

    Performance tests in the DefaultClock's general usecase did not show a significant difference between the implementations with and without the volatile keyword.

remikey avatar Feb 21 '20 10:02 remikey

merged into develop.

remikey avatar Feb 21 '20 12:02 remikey