Boost logo

Boost Users :

Subject: [Boost-users] Troubleshooing deadlock with upgrade_lock<shared_mutex>
From: Ken Smith (smithkl42_at_[hidden])
Date: 2010-04-23 13:30:55


I'm a C++/boost newbie, so my apologies if this is a basic question.

I've got a multithreaded app that appears to be deadlocking when two threads
are trying to acquire an upgrade_lock<shared_mutex> at the same time.
 Specifically, I've got a class that looks something like this:

#include <boost/thread.hpp>
using namespace boost;

class Manager
{

   public:
      Manager();
     ~Manager();
      void createRoom();
      void deleteRoom();
      void transmit();

   private:
      shared_mutex roomManagersMutex;

};

void Manager::createRoom()
{
   upgrade_lock<shared_mutex> upgradeLock(roomManagersMutex);
   // Read stuff

   if(roomShouldBeCreated)
   {
      upgrade_to_unique_lock<shared_mutex> uniqueLock(upgradeLock);
      // Create room
   }
}

void Manager::deleteRoom()
{
   upgrade_lock<shared_mutex> upgradeLock(roomManagersMutex);
   // Read stuff

   if (roomShouldBeDeleted)
   {
      upgrade_to_unique_lock<shared_mutex> uniqueLock(upgradeLock);
      // Delete the room
    }
}

void Manager::transmit()
{
   shared_lock<shared_mutex> lock(roomManagersMutex);
   // Read stuff about the room
}

The Manager::deleteRoom() and Manager::createRoom() methods are typically
only called once every few seconds, but Manager::transmit() is called every
20 milliseconds or so, i.e., about 50 times / second.

Periodically, the app stops responding. When it does, I can see that the
transmit() method is working fine, but the createRoom() and deleteRoom()
methods (on separate threads, obviously) appear to be deadlocked on the
highlighted lines, waiting to acquire their upgrade_locks.

Now that seems weird to me, because I thought the whole purpose of the
upgrade locks was that they're supposed to be shared, right up until you
request an upgrade to a unique lock. Obviously the code is more complex
than what I've described, but it doesn't appear that there's a unique lock
being held anywhere when the deadlock happens.

Am I doing something wrong? Any suggestions on troubleshooting this?

Ken Smith
Cell: 425-443-2359
Email: smithkl42_at_[hidden]
Blog: http://blog.wouldbetheologian.com/



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net