Thread Lock Checker

You’ve written a multi-thread application, you’ve implemented a locking strategy, but data is being corrupted. Why is that happening?

One possible reason is that the locking strategy is faulty. A manual review says it’s good, but real world experience says otherwise. What can cause that?

Many locking primitives have “default unlocked” conditions. If these are setup in the default condition, visually they look OK, but they don’t lock the lock when it is acquired.

void function()
{
	CSingleLock lock(&critSect);
	doProtectedWork();
}

Locks configured in this manner are effectively impotent. You need a tool that can scan for such conditions and report them to you.

Thread Lock Checker is a software tool that scans source code looking for various usage types of single and multiple critical section locking classes, such as CSingleLock and CMultiLock.

Thread Lock Checker

Thread Lock Checker scans all source files under a specified disk hierarchy. When usage types are found that match the criteria specified in the settings the usage is reported on the lock report.

Thread Lock Checker error report

In the lock report shown above 11 matches have been found for CSingleLock criteria that match the settings. These include:

  • Six cases of a CSingleLock created deliberately unlocked.
  • One case of a CSingleLock created unlocked because no locking parameter was specified (and thus the default of unlocked is used).
  • One case of a CSingleLock created and locked by a value specified in a variable. This is marked “Unknown unlocked”.
  • Three cases of CSingleLocks incorrectly being created on the stack but with no variable name declaration. This results in code that will compile but will create a CSingleLock that is immediately destroyed. Thus the lock, although in place can never protect the resource that it is intended to protect.

Thread Lock Checker source code

Clicking on an entry in the lock report will shown the source code in the source code viewer. The line of interest is highlighted in green.

Double clicking on an entry in the lock report will start Visual Studio to edit the source code.

But we have our own locking primitives

Additional class names can be specified so that you can search for your own classes that implement similar behaviour.

Command line

You can automate this checking by using Thread Lock Checker from the command line. Add automated checking to your code review process, source code check in process, or to your software release process.

Example 1 Scan a directory looking for locking errors and output the results to a log file. If there are no results, there will be no log file.

threadLockChecker.exe /dir e:\om\c\
                      /output e:\test.txt

Example 2 Scan a directory looking for only CSingleLock and output the results to a log file. If there are no results, there will be no log file.

threadLockChecker.exe /dir e:\om\c\
                      /output e:\test.txt
                      /removeAllLockTypes /addLockType CSingleLock

Fully functional, free for 30 days