Things which make debugging Multi-Threaded applications more difficult [modified]
-
Thread 1 code sequence goes like this.
AnotherFuncThatGetsExclusiveAccessToTheObject();
ASSERT(IHaveExclusiveAccess());
DoSomethingWithTheObject();
AnotherFuncThatReleasesExclusiveObject();
DoSomeOtherThing();
DoSomethingElseUnrelated();Thread 2 code goes like this:
GetExclusiveAccessToTheObject();
//Make sure that our locking mechanism is working
ASSERT(IHaveExclusiveAccess());DoSomethingWithTheObject();
ReleaseExclusiveObject();The ASSERT's (standard VC++ asserts unmodified) where added to make sure that the locking code worked and only one thread has exclusive access to an object. The idea was that if there is a bug in the locking code and by chance two threads have same access the ASSERT will indicate the error and break in the debugger. The debugger call stack can then be inspected to see what other threads are doing and which other thread accessed the object. However, this never works in practice without writing a custom ASSERT. Why? -- modified at 17:29 Sunday 24th September, 2006
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan