Please enable JavaScript to view this site.

Thread Validator Help

Navigation: Examples

Example Application

Scroll Prev Top Next More

 

The example application

 

The example application is a great way to explore the capabilities of Thread Validator.

 

The source and projects are included in the installation, but you'll need to build the example application yourself.

 

You can then use nativeExample.exe in conjunction with Thread Validator to generate thread errors, waits, deadlocks and potential deadlocks, and monitor the behaviour of the application as you use it.

 

After launching the example application from Thread Validator, the following dialog appears showing counters reflecting the status of various tests you can run from the Test menu.

 

example-application

 

 

How to use these examples

 

The best way to understand how Thread Validator works is by example.

 

We recommend launching the example application from Thread Validator and observing how the menu actions affect threads, locks and synchronization objects.

 

Examining the source code is the best way to see what's going on in the example application.

 

The examples create threads and engage critical sections in different scenarios. For convenience, below we have provided the source locations where each menu action runs a test.

 

Most test locations are in the CTeststakView class of nativeExample\TESTSVW.CPP

 

 

File menu

 

menu File menu instructionStep Exit instructionStep closes the example application

 

 

Test menu

 

example-application-test-menu

 

menu Test menu instructionStep ...

 

instructionStep 2 Thread deadlock

OnHandlesStartthread1()

OnHandlesStartthread2()
 

Creates two named threads that increment counter1 and counter 2 in the midst of repeatedly locking and unlocking two critical sections with an interim Sleep.

 

Sooner or later the two threads will deadlock and the counters being updated in the view will stop.

 

instructionStep 3 Thread deadlock

OnHandlesStartthreadA()

OnHandlesStartthreadB()

OnHandlesStartthreadC()

 

Creates three named threads that increment counterA,B,C in the midst of repeatedly locking and unlocking three critical sections with interim Sleeps.

 

Sooner or later the threads will deadlock and the counters being updated in the view will stop.

 

instructionStep Start 2 Thread deadlock

OnTestStart2ThreadDeadlock()
 

Combines the above 2 Thread deadlock operations into a single function.

 

instructionStep Start 3 Thread deadlock

OnTestStart3threaddeadlock()
 

Creates three named threads that increment counter3A,3B,3C in the midst of repeatedly locking and unlocking two of three critical sections with no interim Sleeps, and with a circular dependency.

 

The threads will deadlock and the counters being updated in the view will stop.

 

instructionStep Start 3 Thread deadlock (two)

OnTestStart3threaddeadlock2()

 
Similar to above but with interim Sleeps and no circular dependency.

 

The threads will still soon deadlock and the counters being updated in the view will stop.

 

instructionStep Start 2 Thread deadlock with infinite wait

OnTestStart3threaddeadlockwithinfinitewait()
 
See comments in this function for an explanation.
Deadlock occurs when CounterIa,Ib stop incrementing.
 

instructionStep Start 2 Thread deadlock with thread suspend

OnTestStart3threaddeadlockwiththreadsuspend()

 
See comments in this function for an explanation.
Deadlock occurs when CounterSa,Sb stop incrementing.

 

instructionStep Start 2 Thread deadlock with infinite sleep

OnTestStart3threaddeadlockwithinfinitesleep()

 

See comments in this function for an explanation.

Deadlock occurs when CounterSleepIa,Ib stop incrementing.

 

instructionStep Bad lock strategy example

OnTestBadlockstrategyexample()

 

Create two threads that both use the same incorrect lock strategy. These two threads exhibit a bad lock strategy, but will probably not deadlock each other for a long time.

 

Deadlock occurs when CounterBa,Bb stop changing.

 

instructionStep Good lock strategy example

OnTestGoodlockstrategyexample()

Create two threads that both uses a good lock strategy. These two threads should never deadlock.

 

Deadlock occurs when CounterGa,Gb,Gc shouldn't stop changing.

instructionStep Good lock strategy example (faster)

OnTestGoodlockstrategyexampleEx()

 

As above but much faster.

 

instructionStep Create deadlock then determine stack traces

OnTestCreatedeadlockthendeterminestacktraces()

 

Forces a deadlock with two threads and leaves them running so that Thread Validator can determine stack traces

 

instructionStep Recursion test

OnTestRecursiontest()

 

Enters the same critical section 3 times in a row.

 

instructionStep Leave non-entered critical section

OnTestLeavenonenteredcriticalsection()

 
Enters a critical section once and then leaves it twice without entering it again.

 

instructionStep Delete still active critical section

OnTestDeletestillactivecriticalsection()

 

Enters a critical section once and then deletes it without leaving.

 

instructionStep Leave critical sections in the wrong order

OnTestLeavecriticalsectionsinthewrongorder()

 

Simple test to lock three sections and then unlock them in the same order instead of the reverse order.

 

instructionStep Potential deadlock 2 threads

OnTestPotentialdeadlock2threads()

 

Two threads enter and leave four critical sections, but two of them are in different orders to the other thread. There is a potential for deadlock here.

 

instructionStep Potential deadlock 3 threads

OnTestPotentialdeadlock3threads()

 

Two threads enter and leave four critical sections, but three of them are in different orders to the other thread. There is a potential for deadlock here.

 

instructionStep Test ExitThread()

OnTestTestexitthread()

 

Creates two named threads, one of which explicitly calls ExitThread.

 

instructionStep Force crash inside a locked critical section (part 1)

OnTestForcecrashinsidecrtleavingapermanentlockoncrtthendeadlock()

 
Arranging a crash inside a critical section while it is locked and failing to unlock it - so as to show the deadlock this can cause (infinite wait on a contended lock) - see part 2.

 

These functions has changed their mechanism due to Microsoft CRT fixes.

instructionStep Force crash inside a locked critical section (part 2)

OnTestForcecrashinsidecrtpart2()

 

When preceded by part 1 (above) this call should deadlock (cause an infinite wait on a contended lock) as the lock is held by the waiting main UI thread that crashed before unlocking the critical section.

 

 

 

Help menu

 

menu Help menu instructionStep ...

 

instructionStep About Thread Validator Tester

TvExampleApp::OnAppAbout()

 

Shows a simple information dialog using code in nativeExample.cpp