3 ms·
Reading ad-hoc is fine if you just want to observe the value at some point in time. That is why there is room for both a “peek” function, maybe should be calle
by spockz 4y ago
Reading ad-hoc is fine if you just want to observe the value at some point in time.
That is why there is room for both a “peek” function, maybe should be called “unsafeRead”, and a “withValue” method that takes a function with the actual value as argument and locks the atomic variable while executing the closure. This prevents the user from ever forgetting to write the lock.
- MauranKilom 4y ago> Reading ad-hoc is fine if you just want to observe the value at some point in time. There are such cases, indeed. Double-checked locking comes to mind. But if the code inside the if does assume the atomic to have a given value, it is almost surely wrong (absent of other synchronization mechanisms, such as in double-checked locking).