Boost logo

Threads-Devel :

Subject: [Threads-devel] shared_mutex documentation request
From: Fredrik Orderud (forderud_at_[hidden])
Date: 2012-08-07 09:23:29

I've recently struggled with a deadlock caused by shared_mutex being
unwilling to allow more readers to enter when a writer is already
waiting (running on Windows 7/x64). Code to reproduce is pasted below.
Prioritization of writers might be a perfectly good strategy, but I
could find no mentioning of it neither in the shared_mutex.hpp headers
nor in the boost::thread documentation webpage.

Would it be possible to extend the shared_mutex documentation to also
cover some implementation details about the reader vs. writer
prioritization scheme (or similar)?

Thanks in advance,
Fredrik Orderud

#include <boost/thread.hpp>
#include <boost/thread/shared_mutex.hpp>

void WriteLockUnlock (boost::shared_mutex * mutex) {
    // acquire write-lock (blocks until no readers)
    boost::unique_lock<boost::shared_mutex> lock(*mutex);

    // release write-lock

int main () {
    boost::shared_mutex mutex;

    boost::thread t;
        // acquire read-lock #1
        boost::shared_lock<boost::shared_mutex> r1_lock(mutex);

        t = boost::thread(WriteLockUnlock, &mutex);

        // acquire read-lock #2 (blocks due to write-lock request)
        boost::shared_lock<boost::shared_mutex> r_lock(mutex);

        // release read-locks #1 & #2


    return 0;

Threads-Devel list run by bdawes at, david.abrahams at, gregod at, cpdaniel at, john at