Dec 30, 2013 at 10:34 PM
Edited Dec 30, 2013 at 10:45 PM
I noticed a deadlock in my application that occurred in Update Controls. Since Update Controls tries to be thread safe, I thought I should mention it. The scenario is this: my program has a "combined search function" that performs fast and slow
searches (the fast search scans in-memory data structures and gives immediate results, the slow search scans disk data structures and takes more time).
So I do the fast search on the UI thread and the slow search on a worker thread. A Dependent updated on the UI thread combines the results:
A B (Independents) C D (Independents)
| | | |
| +-------------------+ |
| | | |
| | +-+-+
| | |
| | v
| | Dependent
| | | Dependent sends
| | ~~~ async message
| | | to worker thread
v v | (with copy of B C D)
A B C D represent the search parameters and search data (B and C are used by both searches while A and D are only used by one search type).
A deadlock happens as follows:
The UI thread updates A, B, C, or D (shouldn't matter which one).
- results1.MakeOutOfDate() is called which locks results1.
- results1.MakeDependentsOutOfDate() calls CombinedResults.MakeOutOfDate() which locks AllResults.
Meanwhile the worker thread changes results2.
- AllResults.MakeOutOfDate() is called, which locks AllResults.
- AllResults.MakeOutOfDate() calls results1.RemoveDependent(this), which locks results1 (DEADLOCK!)
Have you encountered this problem? Do you know of a general guideline for avoiding it?