This tutorial demonstrates Thread Validator detecting your application acquiring locks in an order that does not create a deadlock, but which has the potential to cause a deadlock. This type of locking error can lie dormant in the application until the application is later modified changing a locking order resulting in a deadlock. Sometimes the deadlock will only manifest under particular conditions, often at a customer site.
This type of error is also known as a potential deadlock, as incorrect lock ordering can lead to deadlocks when interacting with other code that uses the locks. Common causes of this type of error are unrelated code sections calling other code sections which are modified to include synchronization without regard for other synchronization already in place. Other causes of this type of error are incorrect logic forgetting to unlock a critical section.
Note that for your executing nativeExample your critical section addresses, thread Ids and sequence numbers will all be different. We have used the values that we experienced to write this tutorial. Substitute your own values where appropriate.
- Enable potential deadlock error detection.
WARNING: For some applications with many threads and many critical sections this option can cripple an application. Use with caution.
Open the settings dialog by clicking on the tools icon on the toolbar.
- In Novice mode, select the Detect Potential Deadlocks check box and click OK.
- In Intermediate mode or Expert mode, select the Detect tab and select the Detect Potential Deadlocks check box and click OK.
- Launch the sample application. Click on the re-launch icon on the toolbar.
- The previously launched application is started.
- From the Test menu, choose the Potential deadlock 2 threads.
- Viewing the Locks tab will show four critical sections in purple. This indicates that each critical section has a thread error.
- Viewing the Per Thread Locks tab will show all application threads, two of which will each have four critical sections highlighted in purple. This indicates that each critical section has a thread error.
- Viewing the Current Locks tab will show several threads. Two threads (marked as potential deadlock in the Owning Module column) will have at least four critical sections. The critical sections will be marked as active (grey), contended (yellow) or potential deadlock (purple).
- Using the Locks tab, right click on the purple entry and choose Information about lock/wait…. A modeless dialog containing information about the lock is displayed. The information includes some text describing the error.
- Using the Per Thread Locks tab, right click on a purple entry and choose Show Callstack…. A modeless dialog containing the callstack for the lock is displayed.
- Repeat this for all of the critical sections that are purple. Using the information in the callstacks you can identify the incorrect locking strategy.
- Close the Application using the File menu’s Exit command.
- The various tabs will display the last known state of the critical sections and waits prior to the application finishing.
- Any errors identified during shutdown will be added to the state for each critical section and the displays updated to reflect this.
- Disable out of Potential Deadlock Detection. Open the settings dialog by clicking on the tools icon on the toolbar. Deselect the Potential Deadlock Detection check box.