Boost logo

Boost :

Subject: [boost] [thread] upgrading read lock to write lock
From: Edd Dawson (lists_at_[hidden])
Date: 2011-02-06 16:11:22


I'm struggling to find the correct incantation to upgrade a (conceptual) read
lock to a write lock.

Here's my best guess at what I think should work, but it deadlocks at the
highlighted point:

#include <iostream>
#include <iomanip>
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread/locks.hpp>

int main()
{
     using namespace boost;

     shared_mutex m;
     shared_lock<shared_mutex> readlock(m);
     // reads go here
     // ...

     // Ah, need to write actually.
     // Upgrade to a write lock now.
     upgrade_lock<shared_mutex> uglock(m);
     std::cout << __LINE__ << std::endl;
     upgrade_to_unique_lock<shared_mutex> writelock(uglock); // <-- deadlock?
     std::cout << __LINE__ << std::endl;

     return 0;
}

I've been trying this with various combinations of msvc 9, msvc 10, mingw 4.3,
boost 1.39 and boost 1.45, but always with the same results.

Could someone kindly put me out of my misery? :)

Kind regards,

Edd


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk