Skip to content

Locking

Nill edited this page Sep 10, 2021 · 1 revision

Locking and synchronizing in Telefrag is rather straightforward and clean:

object thing = new();    // Thing to lock on

using (var l = thing.Lock()) 
{
    // .... we hold a lock on 'thing'
}
// and now the lock has been released

To lock the whole scope:

object thing = new();
using var lock = thing.Lock();
// locked until this scope ends

The return value from Lock() is a LockMonitor<T> which acquires the lock and then releases the lock when the LockMonitor is disposed.

Additionally:

  • ReaderWriterLock and ReaderWriterLockSlim support LockMonitor via the LockRead() and LockWrite() extension methods.
  • You can use LockMonitor for any kind of locking or blocking mechanism:
public LockMonitor<T> CustomLock<T>(T object)
{
    return new LockMonitor<T>(object, 
                              o => customLockRoutine(o),
                              o => customReleaseRoutine(o));
} 

Clone this wiki locally