Discussion: Coding and debugging Multithreaded code.
Posted on 2006-04-18
I'm putting together a tool right now that is using a multithreaded architecture. Its actually in C++ but I'm asking it here because I dont think that is relevant.
As this is probably the first time I actually have an input on the overall design and architecture of a multithreaded app I thought I would canvas some experts! :-)
I am looking more for architectural design and general methods rather than specific techniques for specific problems.
The areas I want to cover are:
What do you avoid or encourage in the overall design? If one thread reads from a structure and one writes to it, how do you ensure that they can both work together in harmony without losing the benefits of multithreading, especially with complex structures such as lists and trees?
There are loads of both obvious and wierd race conditions that can sneak up and grab you. Many can take hours of work to track down. From the obvious log file sharing issues to the complex 'walking a tree while it's being updated'. How do YOU go about tracking down the actual problem, avoiding the old 'well it must be somewhere in here so I'll just close the door on the whole structure with a mutex'.
It's a bit wierd to hit a breakpoint and see the other threads carry on running! How does one freeze the whole system at a break point so you can investigate the state and history of all threads at the exact moment of the problem?
What logging methods do you use to ensure that traceback is possible without losing the bug by changing the thread timings?
As most threading issues arise through rare occurrences, what techniques are good for exposing the issues early in the development cycle?