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. Specifically Invalidate method can be called only during serialized write operation. LazyUpdate thus solves problem when many concurrent readers are possible (but only single writer), lazy update of mutable cache being performed during read operation.
When the cache is in updated state, BeginUpdate is wait-free.
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). Wait-free if cache is updated.
void EndUpdate() const
Signals that the cache was updated.
Sets LazyUpdate into invalid state.