
Hi, I am trying to implement a simple producer/consumer system with two or more threads. The main thread has a consumer class which creates producers. These producers perform synchronous blocking operations then post their outcome into a std::vector of results which was passed from the main consumer. class consumer { public: consumer() { m_results = boost::shared_ptr<std::vector<result> >( new std::vector<result> ); new producer( m_results, m_mutex ); // do a single task and put result in m_results } void Process() { result r; { boost::mutex::scoped_lock lock(m_mutex); r = m_results.pop.front(); } // Do stuff with r } private: boost::mutex m_mutex; boost::shared_ptr<std::vector<result> > m_results; }; class producer { producer( boost::shared_ptr<std::vector<result> > results, boost::mutex& mtx ) { boost::thread( boost::bind( &DoInThread, this, results, mtx ) ); // actually uses a threadpool but not relevant here. } void DoInThread( boost::shared_ptr<std::vector<result> > results, boost::mutex& mtx ) { result r; // do long async op and store in r boost::mutex::scoped_lock lock(mtx); results->push_back(r); } }; /////////////// I am trying to share a single mutex with both producer and consumer. However I get a noncopyable error, even if I pass a reference. Can anyone point out what the correct approach is? Thanks Simon

Simon Pickles <sipickles@googlemail.com> writes:
producer( boost::shared_ptr<std::vector<result> > results, boost::mutex& mtx ) { boost::thread( boost::bind( &DoInThread, this, results, mtx ) ); // actually uses a threadpool but not relevant here. }
I am trying to share a single mutex with both producer and consumer. However I get a noncopyable error, even if I pass a reference.
boost::bind *copies* its parameters, so you will get an error about being unable to copy. You need to use boost::ref: boost::thread( boost::bind( &DoInThread, this, results, boost::ref(mtx) )); Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Anthony Williams wrote:
Simon Pickles <sipickles@googlemail.com> writes:
producer( boost::shared_ptr<std::vector<result> > results, boost::mutex& mtx ) { boost::thread( boost::bind( &DoInThread, this, results, mtx ) ); // actually uses a threadpool but not relevant here. }
I am trying to share a single mutex with both producer and consumer. However I get a noncopyable error, even if I pass a reference.
boost::bind *copies* its parameters, so you will get an error about being unable to copy. You need to use boost::ref:
boost::thread( boost::bind( &DoInThread, this, results, boost::ref(mtx) ));
Anthony
Ah the old friend boost::ref. Thanks anthony

what is the problem here which prevents you from using a pointer? I tend to avoid writeable references, since they obfuscate code. Peter -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Simon Pickles Sent: Wednesday, July 30, 2008 01:21 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Passing Mutexes Anthony Williams wrote:
Simon Pickles <sipickles@googlemail.com> writes:
producer( boost::shared_ptr<std::vector<result> > results, boost::mutex& mtx ) { boost::thread( boost::bind( &DoInThread, this, results, mtx ) ); // actually uses a threadpool but not relevant here. }
I am trying to share a single mutex with both producer and consumer. However I get a noncopyable error, even if I pass a reference.
boost::bind *copies* its parameters, so you will get an error about being unable to copy. You need to use boost::ref:
boost::thread( boost::bind( &DoInThread, this, results, boost::ref(mtx) ));
Anthony
Ah the old friend boost::ref. Thanks anthony _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Simon Pickles wrote:
Hi,
I am trying to implement a simple producer/consumer system with two or more threads.
The main thread has a consumer class which creates producers. These producers perform synchronous blocking operations then post their outcome into a std::vector of results which was passed from the main consumer.
class consumer { public: consumer() { m_results = boost::shared_ptr<std::vector<result> >( new std::vector<result> ); new producer( m_results, m_mutex ); // do a single task and put result in m_results }
void Process() { result r; { boost::mutex::scoped_lock lock(m_mutex); r = m_results.pop.front(); } // Do stuff with r }
private: boost::mutex m_mutex; boost::shared_ptr<std::vector<result> > m_results; };
class producer { producer( boost::shared_ptr<std::vector<result> > results, boost::mutex& mtx ) { boost::thread( boost::bind( &DoInThread, this, results, mtx ) ); // actually uses a threadpool but not relevant here. }
void DoInThread( boost::shared_ptr<std::vector<result> > results, boost::mutex& mtx ) { result r; // do long async op and store in r boost::mutex::scoped_lock lock(mtx); results->push_back(r); } };
///////////////
I am trying to share a single mutex with both producer and consumer. However I get a noncopyable error, even if I pass a reference.
Can anyone point out what the correct approach is?
Thanks
Simon
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Okay, I think my problem is that I cannot pass the mutex reference as an argument in boost::bind. Is there a workaround? Si
participants (3)
-
Anthony Williams
-
peter_foelscheļ¼ agilent.com
-
Simon Pickles