|
Boost Users : |
From: Phil Endecott (spam_from_boost_users_at_[hidden])
Date: 2005-10-30 06:44:10
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
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