Next: , Previous: Concurrent ML, Up: Multithreading


5.5 Pessimistic concurrency

While Scheme48's primitive thread synchronization mechanisms revolve around optimistic concurrency, Scheme48 still provides the more well-known mechanism of pessimistic concurrency, or mutual exclusion, with locks. Note that Scheme48's pessimistic concurrency facilities are discouraged, and very little of the system uses them (at the time this documentation was written, none of the system uses locks), and the pessimistic concurrency libraries are limited to just locks; condition variables are integrated only with optimistic concurrency. Except for inherent applications of pessimistic concurrency, it is usually better to use optimistic concurrency in Scheme48.

These names are exported by the locks structure.

— procedure: make-lock –> lock
— procedure: lock? –> boolean
— procedure: obtain-lock lock –> unspecified
— procedure: maybe-obtain-lock lock –> boolean
— procedure: release-lock lock –> unspecified

Make-lock creates a new lock in the `released' lock state. Lock? is the disjoint type predicate for locks. Obtain-lock atomically checks to see if lock is in the `released' state: if it is, lock is put into the `obtained' lock state; otherwise, obtain-lock waits until lock is ready to be obtained, at which point it is put into the `obtained' lock state. Maybe-obtain-lock atomically checks to see if lock is in the `released' state: if it is, lock is put into the `obtained' lock state, and maybe-obtain-lock returns #t; if it is in the `obtained' state, maybe-obtain-lock immediately returns #f. Release-lock sets lock's state to be `released,' letting the next thread waiting to obtain it do so.