Provide a platform-independent recursive mutex.
More...
Namespaces |
namespace | bslmt |
Detailed Description
- Outline
-
-
- Purpose:
- Provide a platform-independent recursive mutex.
-
- Classes:
-
- See also:
- Component bslmt_mutex
-
- Description:
- 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.
-
- Usage:
- 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.