Thread Validator can display the order in which locks and waits were entered.
The lock order dialog (i.e. Locks and Waits in Sequence Order dialog) can be started in two different ways:
•All Locks tab popup menu Lock Acquisition Order...
• Query menu Display lock order
The dialog displays the order...
•in which critical sections are locked and waited upon
•that waits are entered into
•in which threads sleep and are suspended
For this, you need to enable the Functions working with waitable handles option on the Hook Insertion settings page.
You don't need to enable the individual Sleep option.
•Thread Id displays the thread id and a thread name if one is available
•Address the critical section address that is locked or being waited upon
For WaitForSingleObject calls, this is the handle that is being waited upon.
For WaitForMultipleObjects calls, the number of handles being waited upon is shown.
•Sequence displays the sequence id of the lock or wait
Green bars in this column indicate the sequence id relative to the known range of all ids.
•Status displays the word Locked or Waiting for critical sections
For WaitForSingleObject or WaitForMultipleObjects calls the appropriate function is displayed instead.
•Member variable shows information about the storage variable used when entering the critical section
•Filename displays the filename of the function where the critical section was entered
The image below shows that three threads have deadlocked.
Each thread has acquired one lock and is waiting upon another, so there's two entries in this view for each thread.
The order the threads acquired and is waiting upon locks is the same as the order of the data in the list, which is in turn the same as the sequence ids in the Sequence column.
This situation was caused by the bad lock strategy used in the example program.
In this example three threads are created - lets call them A, B and C - and each repeatedly attempts to lock two of the critical sections a,b and c as follows:
•Thread A locks sections a and b and then unlocks b and a
•Thread B locks sections b and c and then unlocks c and b
•Thread C locks sections c and a and then unlocks a and c
It should be easy to see that a three lock circular deadlock will occur quickly occur as each thread waits for a lock that another thread has, and indeed this is what the image above shows us has happened.
When a sleeping thread is listed:
•the Address column displays Thread
If the Hook Insertion settings has Functions working with waitable handles enabled:
•the Sequence column indicates the sequence id for the start of the thread sleeping
The horizontal position of the green bar in this column changes to show relative size of the sequence id compared to the most recently allocated id.
•the Status column displays Sleeping.
When a suspended thread is listed:
•the Address column displays Thread
If the Hook Insertion settings has Functions working with waitable handles enabled:
•the Sequence column indicates the sequence id for the start of the thread suspension
•the Status column displays Suspended
Selecting a critical section or wait handle in the list highlights (in grey) any other threads using the same critical section or handle (same address).
Any other occurrences of the same thread waiting on a lock or holding a lock will also be highlighted with the thread information colour (orange by default).
•Auto Update refresh the contents of the list once per second
•Refresh refresh the list manually when you need to
The green bars in the Sequence column are automatically updated periodically.
A popup menu is available by right clicking on any item in the list:
•Information about lock/wait... shows the relevant information dialog from those below, depending on the type of item selected
These information dialogs do not block the application so you can show as many as you need, either from this tab or others, and leave them open to compare or investigate later.
•Edit Source Code... opens the default or preferred editor to edit the source code
•Show Creation Callstack... shows the callstack for the creation of this item, i.e. the locked or waiting critical section and thread
•Show Lock Callstack... shows the callstack for the locked or waiting critical section and thread
•Show All Lock Callstacks (this thread)... shows all callstacks for this critical section and only this thread
•Show All Lock Callstacks (all threads)... as above but for any thread