As the title says, what are the compiler, CLR or CPU optimizations to be aware of when working with threads and non-blocking synchronization?
I have read a little about the reordering of instructions to improve efficiency that could break things, and caching optimizations that will result in variables not being visible to other threads right away [0], but are there other (I sure there are) that I need to be aware of?
Any links to recommended reading/blogs/articles/etc will be much appreciated.
Thanks, Egil.
Update: Thanks to Jons link to Joe Duffy’s blog post I found a lot more great information that I thought I would share with you guys:
- The primary article that many point to is Understand the Impact of Low-Lock Techniques in Multithreaded Apps by Vance Morrison.
- What Every Dev Must Know About Multithreaded Apps, also by Vance Morrison, is a very good fresher up for those of us who do not dabble in multithreaded applications daily.
- Joe Duffy’s 9 Reusable Parallel Data Structures and Algorithms is also a great read.
- More of the same from Jeffrey Richter in his Concurrent Affairs column Performance-Conscious Thread Synchronization. His implementation of a SpinWaitLock is pretty nice.
- In general, the Concurrency Affairs columns over at MSDN Magazine are very recommendable.
You need to know about the .NET memory model, basically. Ignore what optimisations are currently performed – code to the model.
I would recommend this blog post by Joe Duffy as a good starting point.
I’d also recommend that you don’t roll your own lock-free code. It’s simply too hard for mortals, IMO. Use frameworks like Parallel Extensions (in .NET 4.0) which do the right thing for you.