|
class LazyUpdate
LazyUpdate class provides thread-safe means for lazy evaluation. Typically it is used in connection with mutable member variable cache. Mutating operations on such class set LazyUpdate to "invalid" state. When any method is called to obtain evaluated value, it first checks LazyUpdate whether cache is valid using BeginUpdate.(such method is usually const, that is why the cache needs to be mutable). If BeginUpdate returns true, cache has to be updated. At the end of update, EndUpdate has to be called.
BeginUpdate also blocks any other thread once update is in progress.
That way, many threads are allowed to invoke read methods simultaneously while updates are performed and serialized correctly. Of course, client code of such class still needs to serialize access to instance, just like for any other object.
void Invalidate()
Sets LazyUpdate to invalid state.
bool BeginUpdate() const
Queries whether LazyUpdate is in invalid state. In that case, true is returned and any other thread calling BeginUpdate is blocked until EndUpdate (and such blocked thread then returns false, as cache is already updated).
void EndUpdate() const
Signals that the cache was updated.
LazyUpdate()
Sets LazyUpdate into invalid state.
|