Leaving a critical section in the wrong order
This tutorial demonstrates Thread Validator detecting your application incorrectly leaving a critical section in the wrong order compared to the order the locks were entered. An example of this error would be to enter locks in the order 1, 2, 3 and leave the locks in the order 1, 2, 3 (the locks should be exited in reverse order – that is: 3, 2, 1).
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 out of order critical section 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 Out of order critical sections check box and click OK.
- In Intermediate mode or Expert mode, select the Detect tab and select the Out of order critical sections 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 Leave critical sections in the wrong order.
- Viewing the Locks tab will show one critical section in purple. This indicates the critical sections has a thread error.
- Viewing the Per Thread Locks tab will show all application threads, one of which will each have one critical section highlighted in purple. This indicates the critical section has a thread error.
- Viewing the Current Locks tab will show two threads. Any critical sections shown will not be coloured with any error colour as the critical section was exited incorrectly and thus is not locked or waiting.
- 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 the purple entry and choose Show Callstack…. A modeless dialog containing the callstack for the lock is displayed.
- 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 order critical section error detection. Open the settings dialog by clicking on the tools icon on the toolbar. Deselect the Out of order critical sections check box.