Effective Concurrency: Apply Critical Sections Consistently

The latest Effective Concurrency column, "Apply Critical Sections Consistently", just went live on DDJ’s site, and will also appear in the print magazine. Here’s a teaser from the article’s intro:

The critical section is our One True Tool for guaranteeing mutual exclusion on mutable shared variables. …

[But critical sections] must be applied consistently, and with the intended meanings. A program must ensure that every use of a mutable shared object is properly protected using exactly one of these mechanisms at any given point in its lifetime. Chaos can erupt if code tries to avoid or invert these meanings (e.g., trying to abuse taking a lock or reading an atomic variable as a "critical section exit" operation; see Example 3), or tries to use inconsistent synchronization techniques at the same time.

I hope you enjoy it.
Next month’s article is already in post-production. It delves into what I think is a really interesting rule, both because it’s widely ignored and because it’s full of surprises: "Avoid Calling Unknown Code While Inside a Critical Section." I’ll blog here when it hits the web about a month from now.
Finally, here are links to previous Effective Concurrency columns (based on the dates they hit the web, not the magazine print issue dates):