ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
Classes | Public Types | Public Member Functions | List of all members
shlublu::MutexLock Class Reference

Recursive mutex. More...

#include <MutexLock.h>

Classes

class  LockingError
 MutexLock throws this exception when a locking or unlocking operation is not legal. More...
 

Public Types

using Guard = std::lock_guard< MutexLock >
 RAII convenience wrapper. More...
 

Public Member Functions

 MutexLock (MutexLock const &)=delete
 Copy constructor is deleted.
 
 MutexLock (MutexLock &&)=delete
 Move constructor is deleted.
 
 MutexLock (bool locked=false)
 Constructor. More...
 
virtual ~MutexLock () noexcept
 Destructor. More...
 
void lock ()
 Locks the mutex once available. More...
 
void unlock ()
 Unlocks the mutex. More...
 
unsigned lockLevel () const
 Returns the lock level of the mutex. More...
 
bool currentThreadIsOwner () const
 Tells whether the current thread owns the mutex. More...
 

Detailed Description

Recursive mutex.

Underlying implementation is based on std::recursive_mutex

Typical examples of use

MutexLock locker; // shared by MyThreadedTransactionsManager instances
// ...
void MyThreadedTransactionsManager::transactionalEnsembleThatDoesNotSupportConcurrency(int someNumericParameter)
{
// ... operations that support concurrency ...
locker.lock();
// ... operations that do not support concurrency ...
for (auto i = 0; i < someNumericParameter; ++i)
{
singleTransaction(i);
}
locker.unlock();
// ... operations that support concurrency ...
}
void MyThreadedTransactionsManager::singleTransaction(int someNumericParameter)
{
// ... operations that support concurrency ...
locker.lock();
// ... operations that do not support concurrency ...
locker.unlock();
// ... operations that support concurrency ...
}

Member Typedef Documentation

◆ Guard

using shlublu::MutexLock::Guard = std::lock_guard<MutexLock>

RAII convenience wrapper.

This wrapper guarantees that the mutex will be unlock at the moment its destructor will be called. Used in a function or method, this makes MutexLock::unlock() to be called at return or throw time with no further manual handling.

Based on the standard std::lock_guard implementation.

See also
std::lock_guard

Example

MutexLock locker; // let's assume this is a memeber of MyThreadedTransactionsManager
// ...
int MyThreadedTransactionsManager::methodThatDoesNotSupportConcurrency()
{
int x(someCall());
// ... operations that do not support concurrency ...
return x;
}

Constructor & Destructor Documentation

◆ MutexLock()

shlublu::MutexLock::MutexLock ( bool  locked = false)

Constructor.

Parameters
lockedInitial state of the mutex.

◆ ~MutexLock()

virtual shlublu::MutexLock::~MutexLock ( )
virtualnoexcept

Destructor.

Unlocks as many time as it has been previously locked.

See also
unlock()

Member Function Documentation

◆ lock()

void shlublu::MutexLock::lock ( )

Locks the mutex once available.

Queues until the mutex is available for locking. A lock is available if:

  • it is in released state
  • it has been previously locked by the same thread as the calling one

◆ unlock()

void shlublu::MutexLock::unlock ( )

Unlocks the mutex.

Exceptions
LockingErrorif the mutex is not locked or if it is locked by another thread

◆ lockLevel()

unsigned shlublu::MutexLock::lockLevel ( ) const

Returns the lock level of the mutex.

Returns
the lock level of the mutex

◆ currentThreadIsOwner()

bool shlublu::MutexLock::currentThreadIsOwner ( ) const

Tells whether the current thread owns the mutex.

Returns
true if the calling thread is the owner, false otherwise.

The documentation for this class was generated from the following file:
shlublu::MutexLock::Guard
std::lock_guard< MutexLock > Guard
RAII convenience wrapper.
Definition: MutexLock.h:88
shlublu::MutexLock::MutexLock
MutexLock(MutexLock const &)=delete
Copy constructor is deleted.