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 at the same time.
Specifically, I've got a class that looks something like this:
#include
using namespace boost;
class Manager
{
public:
Manager();
~Manager();
void createRoom();
void deleteRoom();
void transmit();
private:
shared_mutex roomManagersMutex;
};
void Manager::createRoom()
{
upgrade_lock upgradeLock(roomManagersMutex);
// Read stuff
if(roomShouldBeCreated)
{
upgrade_to_unique_lock uniqueLock(upgradeLock);
// Create room
}
}
void Manager::deleteRoom()
{
upgrade_lock upgradeLock(roomManagersMutex);
// Read stuff
if (roomShouldBeDeleted)
{
upgrade_to_unique_lock uniqueLock(upgradeLock);
// Delete the room
}
}
void Manager::transmit()
{
shared_lock 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@gmail.com
Blog: http://blog.wouldbetheologian.com/