I ran across a great article about how to do multithreading without (or with very few) locks.
If you’ve done concurrency, you already know about locks. You probably also know they’re expensive, and you’ve probably wondered how to squeeze out more performance by avoiding locks. This article tells you how to do it safely — and, more importantly, when to avoid it (which is most of the time).
Warning: this is hardcore geek stuff. I think I understood more than half of it.
Here’s the executive summary: Low-lock multithreading is hard. If you don’t understand everything in the article up to and including a given technique, don’t use it. Processor caches and read and write reordering make it more complicated than you thought it was. (Don’t take my word for it — read the first half of the article, before he even starts outlining the first technique.)
He didn’t say it in the article, but I’ll add my two cents: Never optimize anything (including adding low-lock techniques) until you’ve run a profiler and proven that you know where the bottleneck is. Any optimization without a profile is premature optimization.
Always remember M. A. Jackson’s Two Rules of Optimization:
- Rule 1: Don’t do it.
- Rule 2 (for experts only): Don’t do it yet.