Original Post
Hello.
We are currently working on the multithreading infrastructure of our game engine, and we have come accross the issue of dealing with deadlocks. As long as we can detect them, we can deal with them, but how can we detect them efficiently? The solution we found so far is this:
1. Maintain a wait-dependancy graph, which stores which threads are waiting for which other threads.
2. Derive our own syncronization objects.
3. Override the lock method in each of those objects to do:
. a. Check which threads have a lock on the given synchronization object, with a lock type that is mutually exclusive to the requested lock type. This thread will have to wait for these threads to finish with their lock.
. b. Trace the wait-dependancy data for those threads. If the current thread id is encountered, then we have a deadlock situation, and must terminate.
. c. If we survived step b, initiate the lock and add the current thread to the wait-dependancy graph.
4. Override the unlock method in the sync objects to remove the corresponding thread from the wait-dependancy graph.
Unforetunately, this requires a potentially expensive graph traversal each time a lock is initiated. In particular, if multiple-read-exclusive-write synchronizers are involved, this can get rather nasty, since each write lock request may be waiting for multiple read locks to be removed. Further, this method does not allow the detection of a deadlock stemming from two event objects (more widely known as semaphores, I think) waiting for each other. Then again, I'm not sure any method can detect that kind of deadlock, since it is technically not really a deadlock...
Is there a better way to detect/handle deadlocks, or is this as good as it gets?
Michael K.,
Co-designer and Graphics Programmer of "The Keepers"
We come in peace... surrender or die! [edited by - technobot on June 1, 2004 4:37:10 PM]
We come in peace... surrender or die! [edited by - technobot on June 1, 2004 4:37:10 PM]