
260
CHAPTER 8 THREAD SYNCHRONIZATION
sections, held for a long period of time, defeat concurrency and can impact
performance.
• Associate the mutex directly with the resource it is designed to protect,
possibly in a data structure. (Programs 8–1 and 8–2 use this technique.)
• Document the invariant as precisely as possible, in words or even
as a logical,
or Boolean, expression. The invariant is a property of the protected resource
that you guarantee holds outside the critical code section. An invariant might
be of the form: “the element is i n both or neither list,” “the checksum on the
data buffer is valid,” “the linked l
ist is valid,” or “
.” A prec ...