Darren Neimke points to a great resource about multi-threaded programming in .NET, I took the time to leaf through some of the content – top stuff and very digestible. One thing that I didn’t see in there was the explaination of the double-check case that often needs to be implemented. This PDF file relates to the issue in C++ but if you look at the first diagram on page three you can see a lock with double check implementation.

This was front of mind for me this week after talking about scenario where their cache gets invalidated and all of a sudden they have a rush on their database. In this case a double check is required because each of the concurrent threads with identify that the cache is empty and even if they do lock while one does the fetch all the other threads are queued up on the Monitor/Mutex waiting to do the same thing.

The double check gets around this by throwing those threads back to fetch from the cache as soon as they get past the lock. You trade off some busy thread synchronisation time for a few thousand database hits – line ball decision?