Monitors - General Notes

A monitor is a synchronization object which can be locked for read, browse, and write operations. This type of synchronization is typically used to protect a data structure in which different threads might want to read and write.

A monitor can only be used as an intra-process synchronization object.

Any monitors created or opened using the library routines are automatically closed when the run-time system terminates (for example, as a consequence of a STOP RUN).

A write lock locks out all other read, browse, and write locks. Typically, a write lock is used when writing to the data structure.

A browse lock locks out other write and browse locks, but allows simultaneous read locks. Typically, a browse lock is used when reading a data structure with a view to writing to it depending on the results of the read. To this end, a function is provided to convert a browse lock to a write lock without letting in any other writers.

A read lock only locks out write locks. Typically, a read lock is used when reading a data structure without any intention of writing to it.

A thread that has acquired a read lock on a particular monitor might acquire another read lock on the same monitor. However, when a read lock has been acquired, the thread may not request a browse or write lock, unless an "outer" browse or write lock exists in which the read lock is nested. Any attempt to break this rule will result in an run-time system error. When interleaved priority is requested on a monitor it is possible that a nested read lock will block waiting for a writer to finish his request, but since the first read lock is still active, the writer will never be granted write access - this is single thread deadlock. If you expect nested read locks to be obtained then either use reader priority or obtain a write or browse lock at the highest level of locking.

A thread that has acquired a browse lock on a particular monitor may acquire another browse lock on the same monitor. It may also acquire a write lock. It may also acquire a read lock, as long as that read lock is released before the original browse lock is converted into a write lock, and before any write lock is requested.

A thread that has acquired a write lock on a particular monitor may also acquire another write, browse or read lock on the same monitor.