[threads] read_write_mutex and condtion

Dear All, Is it possible to use a boost::condition in combination with a read_write_mutex? I've tried but failed. This is with 1.32.0. My test code follows. As you can see it can use either a mutex or a read_write_mutex depending on which bits are commented-out. It works as expected with a mutex, but with a read_write_mutex I get a compiler error, shown below. Perhaps I am missing some fundamental reason why conditions can't work with read_write_mutexes. Can someone explain? Thanks, Phil. #include <string> #include <iostream> #include <boost/thread/mutex.hpp> #include <boost/thread/read_write_mutex.hpp> #include <boost/thread/condition.hpp> #include <boost/thread/thread.hpp> #include <boost/bind.hpp> #include <unistd.h> using namespace std; using namespace boost; struct shared_thing { string data; //typedef mutex mx_t; //typedef mutex::scoped_lock read_lock; //typedef mutex::scoped_lock write_lock; typedef read_write_mutex mx_t; typedef read_write_mutex::scoped_read_lock read_lock; typedef read_write_mutex::scoped_write_lock write_lock; mx_t mx; condition cn; shared_thing::shared_thing(void): //mx() mx(read_write_scheduling_policy::writer_priority) {} void writer(void) { while(1) { sleep(1); { write_lock l(mx); data += "."; cn.notify_all(); } } } void reader(void) { while(1) { read_lock l(mx); cn.wait(l); cout << data << endl; } } }; int main(int argc, char* argv[]) { shared_thing t; thread wr_thread(bind(&shared_thing::writer,&t)); t.reader(); } $ g++ -pthread -O -Wall -o condvar condvar.cc -lboost_thread /usr/include/boost/thread/detail/read_write_lock.hpp: In member function `void boost::condition::wait(L&) [with L = boost::detail::thread::scoped_read_lock<boost::read_write_mutex>]': condvar.cc:51: instantiated from here /usr/include/boost/thread/detail/read_write_lock.hpp:297: error: ` boost::read_write_mutex&boost::detail::thread::scoped_read_lock<boost::read_write_mutex>::m_mutex ' is private /usr/include/boost/thread/condition.hpp:92: error: within this context

Phil Endecott wrote:
Dear All,
Is it possible to use a boost::condition in combination with a read_write_mutex? I've tried but failed. This is with 1.32.0. My test code follows. As you can see it can use either a mutex or a read_write_mutex depending on which bits are commented-out. It works as expected with a mutex, but with a read_write_mutex I get a compiler error, shown below.
Perhaps I am missing some fundamental reason why conditions can't work with read_write_mutexes. Can someone explain?
Thanks, Phil.
<snip> I'd ask if the same happens with 1.33.0, but since read_write_mutex is about to be pulled from Boost.Threads anyway (it's got some unrelated issues), it doesn't seem worth it. Just don't use read_write_mutex. -- don't quote this

Simon Buchan wrote:
I'd ask if the same happens with 1.33.0, but since read_write_mutex is about to be pulled from Boost.Threads anyway (it's got some unrelated issues), it doesn't seem worth it. Just don't use read_write_mutex.
That's bad news, where's that being discussed? -- Will Bryant http://carcino.gen.nz/ will@core-dev.co.nz +64 21 655 443
participants (3)
-
Phil Endecott
-
Simon Buchan
-
Will Bryant