Locked (keyword)
This is a Language topic about Oxygene
Language Topics Introduction | Structured Overview | Grammar | Keywords | Functions
The locked directive allows methods to be declared implicitly thread safe. The compiler will automatically generate the proper calls to Monitor surrounding the method body.
method DoItSafely; locked;
Declaring a method as "locked" is basically equivalent to surrounding the entire method body with a "locking" statement:
locking self do begin ... end;
Applies to
Locked On
In version 3, it's possible to enter the object to lock a method, property or event on. The syntax for this is:
method DoItSafely; locked on fLockObject;
Notes
Locked is only allowed on non-virtual and final methods. Locked operates at the class level, so methods will lock each other.
See the Microsoft documentation on Monitor Class for further information regarding locking. Their docs state:
"The Monitor class controls access to objects by granting a lock for an object to a single thread. Object locks provide the ability to restrict access to a block of code, commonly called a critical section. While a thread owns the lock for an object, no other thread can acquire that lock. You can also use Monitor to ensure that no other thread is allowed to access a section of application code being executed by the lock owner, unless the other thread is executing the code using a different locked object."