Custom locking mechanism

It is actually not that hard to implement a lock pattern in our code without using any external implementation. We only need a little help in the form of the TInterlocked class that I mentioned previously. The Lock (custom) button on the demonstration program activates two methods that use this approach.

The code uses an integer field called FCustomLock: integer as a lock. The logic is very simple; when this field contains a value of 0, access is granted. If the value is 1, access is locked.

The main program creates two threads, one executing CustomLockIncrementValue and another executing CustomLockDecrementingValue. The former is shown here:

procedure TfrmLock.CustomLockIncrementValue;var  i: integer;begin for i := ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.