BDE 4.14.0 Production release
|
Provide a platform-independent recursive mutex.
This component provides a mutually exclusive lock primitive ("mutex") that is "recursive" - a given thread can lock a recursive mutex multiple times, and then release it by unlocking it the same number of times. The bslmt::RecursiveMutex
class provides the following operations: lock
, tryLock
, and unlock
.
The non-recursive mutex bslmt::Mutex
has substantially lower overhead than bslmt::RecursiveMutex
, and should be used instead if at all possible. In particular, it is rare to need a recursive mutex.
The behavior is undefined if unlock
is invoked on a bslmt::RecursiveMutex
object from a thread that does not currently own the lock.
This section illustrates intended use of this component.
As the name implies, bslmt::RecursiveMutex
supports multiple calls to lock
, which must be balanced by a corresponding number of calls to unlock
. Suppose that we are using a bslmt::RecursiveMutex
object to guarantee exclusive access to some object. The following sketches the "recursive" nature of bslmt::RecursiveMutex
:
Assume that we do not have exclusive access to the object.
We have exclusive access here.
We still have exclusive access.
We still have exclusive access.
The two calls to unlock
have balanced the two earlier calls to lock
. Consequently, we no longer have exclusive access.
Note that bslmt::RecursiveMutex
has substantially more overhead than does bslmt::Mutex
. Consequently, the latter should be used unless recursive locking is truly warranted.