Protecting a Shared Record with a Lock

A lock can restrict a critical section to one participating worker at a time. Every relevant access needs to follow the supported protocol.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a two-field update
  2. Keep locked work scoped
  3. Protect a multi-field invariant from simultaneous changes
  4. Decide which operations share the protection

Trace a two-field update

A queue changes both an items list and a separate count. Another worker should not observe a new count with the old list if the interface promises consistency. Representation invariants identify the shared property.

Keep locked work scoped

Operations that wait on unrelated tasks can increase contention or introduce dependencies. Check the required observations before moving an action outside the section. Lock ordering illustrates a related wait concern.[1]

Protect a multi-field invariant from simultaneous changes

A shared record stores count and total. Updating one before the other can allow a reader to observe a count from one version and total from another. If all accesses participate in a suitable lock or snapshot protocol, a reader can obtain a coherent pair. See row locks and dependent changes.

Old pair: count 2, total 10
Writer adds value 8 → new pair count 3, total 18
Partial observation count 3, total 10 would yield wrong mean

Decide which operations share the protection

Protecting only writes can leave readers observing intermediate state. Keep work inside the protected region to what the invariant needs. Test an overlapping reader and writer.

References

  1. OpenMP Architecture Review Board, OpenMP API Specifications ↩