|
Threads-Devel : |
From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2007-07-02 03:02:52
rick68 <rick68_at_[hidden]> writes:
> I try timed_mutex whit linux, but it not work.
> void timed_mutex::do_lock()
> {
> int res = 0;
> res = pthread_mutex_lock(&m_mutex);
> assert(res == 0);
>
> while (m_locked)
> {
> res = pthread_cond_wait(&m_condition, &m_mutex);
> assert(res == 0);
> }
>
> assert(!m_locked);
> m_locked = true;
>
> res = pthread_mutex_unlock(&m_mutex);
> assert(res == 0);
> }
> All while-loop never running, because of
> boost::timed_mutex::scoped_timed_lock will check m_locked is false.
Yes. The loops are there so that if the mutex is already locked (m_locked is
true), do_lock will wait for the lock to become available. If the mutex is
unlocked (m_locked is false) then do_lock doesn't have to wait, so doesn't run
the while loop.
> So, I change while-loop to do-while-loop:
>
> ============================================
> void timed_mutex::do_lock()
> {
> int res = 0;
> res = pthread_mutex_lock(&m_mutex);
> assert(res == 0);
>
> do
> {
> res = pthread_cond_wait(&m_condition, &m_mutex);
> assert(res == 0);
> } while (res);
>
> assert(!m_locked);
> m_locked = true;
>
> res = pthread_mutex_unlock(&m_mutex);
> assert(res == 0);
> }
This will deadlock! If there is no other thread holding the mutex (m_locked is
false) then this will enter the condition wait, but no thread will notify the
condition: deadlock.
> It's work very well~ :)
What exactly are you trying to achieve? If you let us know what you want to do
(in your code), then someone might be able to propose a way of achieving that
without breaking timed_mutex.
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