Boost logo

Boost Users :

Subject: [Boost-users] [Interprocess]: using condition variables with readers-writer lock (named_sharable_mutex)
From: Chris Evans (chris.evans_at_[hidden])
Date: 2016-09-20 10:42:46


Hi,

I am trying to use condition variables to signify updated data in a
managed_shared_memory segment. I have one "writer" and multiple "readers"
of the shared state, so I am using a readers-writer lock as follows:

using SharedMutex = bi::named_sharable_mutex;
using ReadLock = bi::sharable_lock<SharedMutex>;
using WriteLock = bi::scoped_lock<SharedMutex>;
using NewEntryCondition = bi::named_condition_any;

// With a "writer" block as follows:

{
    WriteLock w_lock( sh_mut_ );

    // Update the shared memory object

    sh_cond_.notify_all();
}

// And a "reader" block as follows (many processes call the following
simultaneously):

{
    ReadLock r_lock( sh_mut_ );

    sh_cond_.wait(
            r_lock,
            []() {
                std::cout << "checking predicate..." << std::endl;
                // predicate logic goes here...
            });

    // Access object in shared memory here
}

Unfortunately, although the code compiles, the reader processes are never
awakened from wait() and block forever.

According to the following thread from 2011:

http://lists.boost.org/boost-users/2011/02/66053.php

"POSIX does not support condition
variables with read-write locks (although I think Windows Vista does) so
Interprocess does not offer any support for this."

At the time, it appears named_condition_any didn't yet exist. Has the
status of this changed in the last 5 years? Is there a suggested way of
implementing readers-writer locks with condition variables using
boost::interprocess?

Thanks!

-- 
Chris Evans
Systems Engineer
Azure Summit Technology, Inc.
3050 Chain Bridge Road, Suite 600
Fairfax, VA 22030


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