So I added a ParallelExample project to the solution and started experimenting. The end result is an easy to use class called SequentialActionQueue. Usually when you need to synchronize access to a resource, you end up putting a lock section around your code so that only one thread may access it at a time. However things get more complex if you need to synchronize multiple sections of the code (ex. multiple methods in a class where only one may run at a time). This is classic thread-safety issues, and the dragons that lie in those waters. However, With the SequentialActionQueue, you can simply instantiate the class and proxy all actions through it and be guaranteed that everything will run safely one after the other.
The above is a simplified example which emulates the ability to use Interlocked to safely increment/decrement a number. I'd love to get some thoughts on the class ... I wrote a few tests in the sample project, but that's not to say there won't be bugs in it, would love to hear your feedback :-)private SequentialActionQueue queue = new SequentialActionQueue(); private int counter =0;
public void Add(int i) { queue.Enqueue(() => counter += i); }
public void Subtract(int i) { queue.Enqueue(() => counter -= i); }