|
Boost Users : |
From: Feldman, Yulik (yulik.feldman_at_[hidden])
Date: 2003-05-03 11:10:06
Hi,
It looks that there is a bug in
.../boost_1_30_0/libs/thread/src/recursive_mutex.cpp. The bug shows in the
pthreads implementation of the recursive_mutex when the mutex is used with
the "condition" synchronization primitive. Here is the description of the
bug:
When the client code calls wait() function on a condition sending as a
parameter a lock object associated with a recursive_mutex
("m_Condition.wait(Lock)"), the following functions get called:
boost::condition::wait(L& Lock) at condition.hpp:91
boost::condition::do_wait(M& mutex) at condition.hpp:137
boost::detail::thread::lock_ops<boost::recursive_mutex>::unlock(Mutex& m,
lock_state& state) at lock.hpp:54
boost::recursive_mutex::do_unlock(cv_state& state)
The implementation of the last function on "winthreads" and "mptasks" resets
the 'm_count' data member to zero. The "pthreads" implementation at
recursive_mutex.cpp:399 doesn't reset the 'm_count' to zero. As a result,
when the calling thread enters pthread_cond_wait() and the mutex is unlocked
by the pthreads the 'm_count' still remains non-zero. Starting from this
point, the things may go bad. Consider, for example, that another thread
tries to acquire the mutex. The thread calls:
boost::recursive_mutex::scoped_lock Lock(m_Mutex)
boost::detail::thread::scoped_lock<boost::recursive_mutex>::scoped_lock<boos
t::recursive_mutex>(Mutex& mx, initially_locked = true) at lock.hpp:66
boost::detail::thread::scoped_lock<boost::recursive_mutex>::lock() at
lock.hpp:76
boost::detail::thread::lock_ops<boost::recursive_mutex>::lock(Mutex& m) at
lock.hpp:34
boost::recursive_mutex::do_lock() at recursive_mutex.cpp:307
The last function locks the mutex again, and, if
BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE is defined, increments the counter to 2.
Now, since the count is more than 1, the function unlocks the mutex. As a
result, the mutex remains unlocked, after the lock function is called.
The same problem exists in recursive_try_mutex and recursive_timed_mutex.
I've tried to fix the code as follows, which seemed to help in my particular
case:
----------------------------------------------------------------------------
------------------------------------
*** recursive_mutex.cpp.orig Sun Feb 23 19:38:11 2003
--- recursive_mutex.cpp Sat May 3 17:32:44 2003
***************
*** 412,417 ****
--- 412,418 ----
state.pmutex = &m_mutex;
state.count = m_count;
+ m_count = 0;
}
recursive_try_mutex::recursive_try_mutex()
***************
*** 608,613 ****
--- 609,615 ----
state.pmutex = &m_mutex;
state.count = m_count;
+ m_count = 0;
}
recursive_timed_mutex::recursive_timed_mutex()
***************
*** 787,792 ****
--- 789,795 ----
state.pmutex = &m_mutex;
state.count = m_count;
+ m_count = 0;
}
#elif defined(BOOST_HAS_MPTASKS)
----------------------------------------------------------------------------
------------------------------------
Any comments? Is this indeed a bug? Is the fix correct?
Thanks,
Yulik.
[Non-text portions of this message have been removed]
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