Provide a platform-independent mutex for use below bslmt
.
More...
Namespaces |
namespace | bsls |
Detailed Description
- Outline
-
-
- Purpose:
- Provide a platform-independent mutex for use below
bslmt
.
-
- Classes:
-
- See also:
- Component bslmt_mutex
-
- Description:
- This component provides a mutually exclusive lock primitive ("mutex") by wrapping a suitable platform-specific mechanism. The
bsls::BslLock
class provides lock
and unlock
operations. Note that bsls::BslLock
is not intended for direct client use; see bslmt_mutex
instead. Also note that bsls::BslLock
is not recursive.
- This component also provides the
bsls::BslLockGuard
class, a mechanism that follows the RAII idiom for automatically acquiring and releasing the lock on an associated bsls::BslLock
object. To ensure exception safety, client code should make use of a bsls::BslLockGuard
object wherever appropriate rather than calling the methods on the associated bsls::BslLock
object directly.
-
- Usage:
- In this section we show intended use of this component.
-
- Example 1: Using bsls::BslLock to Make a class Thread-Safe:
- In this example we illustrate the use of
bsls::BslLock
and bsls::BslLockGuard
to write a thread-safe class.
- First, we provide an elided definition of the
my_Account
class. Note the d_lock
data member of type bsls::BslLock
: class my_Account {
double d_money;
mutable bsls::BslLock d_lock;
public:
void deposit(double amount);
int withdraw(double amount);
};
Next, we show the implementation of the two my_Account
manipulators show-casing the use of bsls::BslLock
and bsls::BslLockGuard
:
void my_Account::deposit(double amount)
{
Here, we use the interface of bsls::BslLock
directly. However, wherever appropriate, a bsls::BslLockGuard
object should be used instead to ensure that an acquired lock is always properly released, even if an exception is thrown: d_lock.lock();
d_money += amount;
d_lock.unlock();
}
In contrast, withdraw
uses a bsls::BslLockGuard
to automatically acquire and release the lock. The lock is acquired as a side-effect of the construction of guard
, and released when guard
is destroyed upon returning from the function: int my_Account::withdraw(double amount)
{
bsls::BslLockGuard guard(&d_lock);
if (amount <= d_money) {
d_money -= amount;
return 0;
}
else {
return -1;
}
}