Read-write locks
Locks

Prev:Mutual-exclusion locks


A read-write lock encapsulates a common pattern in the implementation of concurrent data structures, where one wishes to ensure that a thread is able to see a consistent view of the structure for a period of time, during which no other thread modifies the structure.

While this can be achieved with a simple Mutual-exclusion locks, performance will suffer, since in fact multiple threads can view the structure at the same time; serialization must only be enforced for writes.

Read/write locks allow any number of threads to hold the read lock simultaneously, however attempting to acquire a write lock blocks until all other threads release read locks and write locks.

Read/write locks are reentrant. A thread holding a write lock may acquire a read lock or a write lock without blocking. However a thread holding a read lock may not acquire a write lock recursively since that could break invariants assumed by the code executing with the read lock held.
rw-lock

<rw-lock> ( -- lock )

with-read-lock ( lock quot -- )

with-write-lock ( lock quot -- )


Versions of the above that take a timeout duration:
with-read-lock-timeout ( lock timeout quot -- )

with-write-lock-timeout ( lock timeout quot -- )