Skip to content

Threading ▪ Locks ▪ AsyncReaderWriterLock

Squared Infinity edited this page May 22, 2017 · 6 revisions

AsyncReaderWriterLock

AsyncReaderWriterLock provides an exclusive access to critical section of code. It allows single exclusive Writer or multiple concurrent Readers. Writers have priority over readers. New readers will be queued and allowed access only when all waiting Writers have already executed.

var l = new AsyncReaderWriterLock();

using (await l.AcquireWriteLockAsync())
{
    //  no other thread will have concurrent access to this critical section

    // it is safe to use async code inside critical section
    await Task.Delay(500);

    // but be careful, you may now be on a different thread than thread which acquired the lock in a first place.
}

Clone this wiki locally