Boost logo

Boost :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2007-03-27 05:30:45


"Steve Weber" <steve.weber.junk_at_[hidden]> writes:

> Im kinda noob only 2 years of c++
>
> When joining two threads using a recursive_mutex. The recursive_mutex is
> not shared resulting in dead lock.
> Is this a "bug" or am i missing something.

You are missing something: you are trying to join whilst holding the mutex
that the thread you are waiting for is trying to acquire.

Also, a plain mutex will work fine here.

> also what would be a good solution to this?

Unlock the mutex before you try and join. As a rule of thumb, you shouldn't do
blocking operations (e.g. join) whilst you have a mutex locked.

> Example code of my problem.
> ----------------------------------------------------------------------
>
> class Test{
> public:
> boost::recursive_mutex m_mutex;
> boost::thread* m_thread;
> bool m_endThread;
> // start thread
> Test() : m_endThread( false ) {
> m_thread = new boost::thread( boost::bind( &Test::threaded, this )
> );
> }
> // cleanup
> virtual ~Test() {
> boost::recursive_mutex::scoped_lock l( m_mutex );
> m_endThread = true;
> m_thread->join(); //< DEAD LOCK

> delete m_thread;
> }
> // the thread that will block
> void threaded() {
> for (;;){
> boost::recursive_mutex::scoped_lock l( m_mutex ); //< DEAD LOCK
> if ( m_endThread ) return;
> }
> }
> };
>
> int main() {
> Test t;
> }
>
> ----------------------------------------------------------------------
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>

Anthony

-- 
Anthony Williams
Just Software Solutions Ltd - http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk