Back in October 2005 I wrote a MSDN article entitled Understand the Impact of Low-Lock Techniques in Multithreaded Apps. This is a follow-on article to the more fundamental article What Every Dev Must Know About Multithreaded Apps, which I mentioned in my last blog post.
Like the ‘What every Dev Must know…’ article, I this article is now much more difficult to access. MSDN articles over 8 years old are not directly accessible but you have to download the whole issue as a CHM file, remove the ‘blocking’ bit, and then extract the file. Not super-hard, but not easy either. To fix this I have done that for you and attached the resulting PDF file to this blog post. Now you can simply open it to see this article. Like its companion article, although it was written over 10 years ago, the information in it is still correct and relevant.
In the ‘What Every Dev Must Know’ article I talk about the need to use locks in a disciplined way to make your program safe in a multi-threaded environment. However locks can be expensive, and so there is a natural desire to avoid them in some high performance cases. Avoiding locks is what I call ‘low lock programing’ because it does not typically eliminate locks, but it takes them off the most critical performance paths in your program. Typically use volatile declarations as well as Interlocked operations (also sometimes called Compare and Swap (CAS)). It is VERY EASY TO GET THIS STUFF WRONG and thus introduce very subtle errors that will manifest as non-deterministic crashes of your application.
In this article I go over the theory of low lock programming, and again try to give you some rules that will keep you safe. The general advice is first ‘use locks’. But if you are ‘forced’ to use low lock programming because of performance concerns on CERTAIN PATHS, you should definitely read this article before undertaking the task. You need to be very careful, and this article gives you the information you will need to avoid subtle races.
P.S: sadly the tool that prints the document to PDF simply omits figures that are ‘collapses’ and it turns out that table2 in the document is collapsed so did not get printed.
I am seeing what I can do about it, but in the mean time here is table 2 in the document below.
You can also get the document yourself as a CHM file. First download the issue MSDN October 2015. You must save it to your disk, and then ‘unblock’ it by right clicking on the downloaded file in windows file explorer, right clicking on it, select properties, and then select the ‘unblock’ checkbox. This marks the file as ‘safe’ so that the normal windows CHM file viewer will work properly. Then you can find the article as well as the table in its proper place.