I am currently involved in a very interesting project which utilizes a massive load of threads and in-memory data structures. The downside if this is of course: how on earth do I make that thread-safe?
Along the journey I stumbled upon Ralf Westphals thoughts and code on that matter. Ralf comprehensively writes about a technology called “Software Transactional Memory“. “What’s that?” you may ask. And here’s the answer provided by the wikipedia:
“In computer science, software transactional memory (STM) is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. It functions as an alternative to lock-based synchronization, and is typically implemented in a lock-free way. A transaction in this context is a piece of code that executes a series of reads and writes to shared memory. These reads and writes logically occur at a single instant in time; intermediate states are not visible to other (successful) transactions. The idea of providing hardware support for transactions originated in a 1986 paper and patent by Tom Knight. The idea was popularized by Maurice Herlihy and J. Eliot B. Moss. In 1995 Nir Shavit and Dan Touitou extended this idea to software-only transactional memory (STM). STM has recently been the focus of intense research and support for practical implementations is growing.”
It seems that this different approach to in-memory data management could a solution for most of the difficulties I and maybe you might have with thread safe programming. Even better: it make the code easier to read and is completely written in C#.
Since I am still in the evaluation phase if or if not to use the NSTM implementation, I will keep you posted on that matter in future updates.
Source 1: http://weblogs.asp.net/ralfw/archive/tags/Software+Transactional+Memory/default.aspx
Source 2: http://en.wikipedia.org/wiki/Software_transactional_memory
Source 3: http://www.codeplex.com/NetSTM