views:

124

answers:

1

What are some of the techniques available to debug multi threading issues in .net environment.

+1  A: 

Debugging and unit-testing multithreaded code is notoriously difficult, and I don't believe there are any tried and true solutions to debugging multiple threads.

However, you can make it easier to write multithreaded code by taking a few notes from functional programming languages:

Use immutable data structures:

its very easy to write multithreaded code in F#, Haskell, Erlang, OCaml, etc because data structures and values in these languages are immutable, meaning that you can't modify a variable once it has been assigned. Essentially, this means every variable is declared constant, and there are no methods like List.Add or Dictionary.Add which mutate the collection.

Immuability means that nothing can be mutated after its been declared, therefore you never have to worry about one thread mutating shared state used by another thread. Since that's the case, you don't have to use locks or mutexes in your code, and you've eliminated a whole class of threading errors related to race conditions and dead locks.

Rely on message passing to share data between process Read these blog post:

Erlang is a remarkable language, specifically because its concurrency model is scalable up to 1000s and 1000s of computer. Rather than giving threads access to shared state, it relies on message passing to send information from one thread to another.

Rely on channels to syncronize threads Here is a nice video on the Newsqueak language: http://video.google.com/videoplay?docid=810232012617965344

It describes Newsqueak's interesting approach to concurrency. It uses objects called "channels" to pipe data from one thread to another. Its easy to find C# implementations of channels.

Remember, you can learn a lot about solving problems in .NET by studying how the same problems are solved in other languages.

Juliet