The first half may be familiar to many (most?) readers,
but there's an interesting exercise at the bottom.
A very useful pattern for the Interlocked* functions is
lock-free lazy initialization via
Yes, that's a really long function name, but it turns out
every part of it ...