Home -> ReadWriteMutex

Declaration

class ReadWriteMutex;

Description

The ReadWriteMutex type is a helper type only, and does not feature on the renderer interface itself. This is an optional type which is available for use by the calling application to assist with threading. As detailed in the Threading Model section, the StartNewFrame method on the IRenderer interface must be called in complete exclusion to all others, on all objects. This allows types on the renderer to operate largely lock free, and be as lightweight as possible. The tradeoff is this one synchronisation requirement is pushed back on the application. If the application due to design never has a chance of overlapping a call to StartNewFrame with other API calls on another thread, no further synchronisation is required. If this is possible however, the ReadWriteMutex is provided to assist with this case.

This class implements a "many concurrent readers, one exclusive writer" lock pattern, which allows a writer to "starve out" read requests until it obtains the lock. This allows the application to satisfy the synchronisation requirements by obtaining read locks before each block of API calls, and a write lock before calling StartNewFrame. This gives low overhead, while allowing multiple threads to access the API in parallel on separate threads, without allowing thread starvation on the thread that calls StartNewFrame. Read locks are also re-entrant on the same thread for simplicity.

WarningWarning

Read locks are re-entrant, however that is only guaranteed to work if each read lock on a given thread is released in the reverse order they are taken.

Members

Nested types

Name Description
Public Class ReadLock
A scoped ReadLock type, which releases the lock when the object is destroyed.
Public Class WriteLock
A scoped WriteLock type, which releases the lock when the object is destroyed.

Lock methods

Name Description
Public member ObtainReadLock
Obtains a read lock for this mutex. This method blocks the calling thread until the lock can be obtained.
Public member ObtainWriteLock
Obtains a write lock for this mutex. This method blocks the calling thread until the lock can be obtained.
Public member ReadLock.Unlock
Releases the read lock manually. This allows a lock to be released early, however the lock will also be released when the lock object is destroyed.
Public member WriteLock.Unlock
Releases the write lock manually. This allows a lock to be released early, however the lock will also be released when the lock object is destroyed.