Optimistic locking

We saw before that locks can sometimes be replaced with interlocked operations and we also used such operations to implement custom-made locking. Interlocked operations are also the basis for the optimistic locking pattern, which can be used to implement changes in shared data without using a classical locking mechanism.

Optimistic locking works when a chance of conflict between threads is very low. Like a database transaction mechanism, optimistic locking assumes that there will be no problem and applies required modifications on its own copy of the data (creates a new object, for example). In the second step, optimistic locking tries to commit the change. With one atomic step (usually implemented with an interlocked instruction), ...

Get Mastering Delphi Programming: A Complete Reference Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.