ThreadMentor: Visualizing Mutex Locks

Three windows are important for visualizing the execution behavior of mutex locks. They are the main window, History Graph window and Mutex window. We have discussed the main window and History Graph window on a previous page, and will only present here those activities that are directly related to mutex locks.

The Main Window

Click on the Mutex button in the upper-left corner to show all existing mutex locks in the system as shown below. Note that only the mutex locks created so far will be shown in the display area, and newly created mutex locks will be shown on-the-fly.

The display area is divided into three columns. The first column has the names of all created mutex locks, the second indicates the number of threads waiting on a mutex lock, and the third lists the ownership. If a lock currently has no owner, the owner column will display --. Thus, in the screen-shot above, we have five mutex locks ChopStick0, ChopStick1, ChopStick2, ChopStick3 and ChopStick4, owned by threads Philosopher0, Philosopher1, Philosopher2, Philosopher3 and Philosopher4, respectively. All mutex locks have exactly one waiting thread.

The History Graph Window

The activities of a mutex lock are shown with three tags MW, ML and MU. See the screen-shot below. Tag MW indicates the thread starts to wait on a mutex lock at that moment, tag ML indicates the thread successfully locks a mutex lock, and tag MU indicates the thread unlocks a mutex lock. Note that these tags do not tell which mutex lock is involved in these three activities. To see the involved mutex lock, click on a tag to bring up the source window. The line that contains the corresponding Lock() or Unlock() call is highlighted.

Normally, when a thread tries to lock a mutex lock, a MW will be shown if the lock has already been locked (or owned) by another thread. When the lock is unlocked and this thread is allowed to lock it, this thread becomes the owner and the lock is locked. At this moment, a ML tag is shown. Note that between tags MW and ML, the history bar of this thread is shown in red. Of course, if this thread is the first thread that can reach and lock the mutex lock immediately, tag MW may not appear. When the mutex lock is released (or unlocked), tag MU is shown. Therefore, between tags ML and MU, the history bar is shown in green (i.e., running) unless this thread is involved in other synchronization activities.

Unlocking (or releasing) a lock will allow another thread to lock it. Once we see a MU tag, we can expect that another thread will have a successful lock acquisition and a ML will be shown. To see the connection between the releasing thread and the acquiring thread, click on the Mutex Locks button in the middle of the History Graph window. Then, you will see some black line segments connecting MU tags and ML tags. In the above screen-shot, we see three such connection lines. The first one links a MU tag of thread Lefty4 and a ML tag of thread Lefty3. This means that thread Lefty4 releases a lock, which is then locked (or acquired) by thread Lefty3. The second connection line indicates that thread Lefty3 releases a lock, which is then locked by thread Lefty2 a little later. The third connection line indicates that thread Lefty3 releases its second lock, which is then acquired by thread Lefty4.

With this capability, you will be able to see clearly the lock exchange, which will make debugging easier.

The Mutex Windows

The two windows discussed above provide a global view of the mutex lock activities. To see the micro part of each mutex lock, click on the line (in the display area of the main window) that contains the mutex lock you wish to examine. This will bring up a Mutex window of that mutex lock. The window below is brought up by clicking on the line that contains the mutex lock name of ChopStick3. You can bring up as many mutex windows as you want.

The name of this window is the name of the corresponding mutex lock. Therefore, this window is named as ChopStick3. In this way, you can easily identify the window you want. The top part has two lock icons and the name of the owner. If the mutex lock is currently locked (i.e., owned by a thread), the lock icons show two "locked" locks and the owner name is shown between these two icons. If the lock is not owned (i.e., unlocked), the two lock icons will show two "open" locks, and there is no owner name. If there are threads waiting on the mutex lock, their names will be shown in the display area. Thus, in the above screen-shot, thread Philosopher3 owns mutex lock ChopStick3, and thread Philosopher2 is waiting on the lock.

Note that it is possible that the display area shows some waiting threads and the lock is not locked. This is because between unlocking and locking there is a time gap (we have only one CPU). Therefore, after the previous owner releases the lock, it takes some time to allow the next thread to acquire the lock, and, as a result, in the Mutex window, you may see waiting threads even though the lock is not locked!