Locking Expressions

A locking expression works much like a regular locking statement, in that it protects a bit of code to be only run from a single thread at a time. In contrast to a locking statement, which can wrap a single code statement or a full block, a locking expression wraps access to a single expression, and its value can be used in a wider expression outside of the scope of the lock.

Example:

var lCount := locking self do CountItemsInList();
// go on to use lOunt, unlocked

Without support for locking as an expression, the above code would have to clumsily declare a manually typed variable first, and then obtained the count, e.g. as such:

var lCount: Integer;
locking self do begin
  lCount := CountItemsInList();
end;
// go on to use lCount, unlocked

Limitation on Island

Like locking Statements, locking expressions are limited to work on Monitor classes, on the Island-based platforms. On the other platforms, any type can be locked on.

See Also