Jpp  15.0.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
JSYNCHRONIZATION::JBasicScopedLock< Lockable > Class Template Reference

Scoped lock. More...

#include <JMutex.hh>

Public Member Functions

 JBasicScopedLock (Lockable &lock)
 Constructor. More...
 
 ~JBasicScopedLock ()
 Destructor. More...
 
int unlock ()
 Unlock the mutex. More...
 

Private Member Functions

 JBasicScopedLock (const JBasicScopedLock &)
 Neither copy-constructible nor copy-assignable. More...
 
JBasicScopedLock operator= (const JBasicScopedLock &)
 

Private Attributes

Lockable * m_lock
 

Detailed Description

template<typename Lockable>
class JSYNCHRONIZATION::JBasicScopedLock< Lockable >

Scoped lock.

The main purpose of this class is to provide an exception-safe locking facility. The lock on the mutex is acquired during construction, while the unlock is performed in the destructor.

Es: Case 1, without scoped lock: using namespace JSYNCHRONIZATION; JMutex mutex;

{ mutex.lock(); throwing_function(variable_to_protect); // <- this shall throw mutex.lock(); // <- this call is never // executed -> deadlock }

Case 2, with scoped lock: using namespace JSYNCHRONIZATION; JMutex mutex;

{ JScopedLock(mutex); throwing_function(variable_to_protect); // <- this shall throw } // the lock is released here automatically

Definition at line 47 of file JMutex.hh.

Constructor & Destructor Documentation

template<typename Lockable >
JSYNCHRONIZATION::JBasicScopedLock< Lockable >::JBasicScopedLock ( const JBasicScopedLock< Lockable > &  )
private

Neither copy-constructible nor copy-assignable.

template<typename Lockable >
JSYNCHRONIZATION::JBasicScopedLock< Lockable >::JBasicScopedLock ( Lockable &  lock)
inlineexplicit

Constructor.

Parameters
locka lockable object (e.g. JScopedLock)

Definition at line 63 of file JMutex.hh.

64  :
65  m_lock(&lock)
66  {
67  m_lock->lock();
68  }
template<typename Lockable >
JSYNCHRONIZATION::JBasicScopedLock< Lockable >::~JBasicScopedLock ( )
inline

Destructor.

Definition at line 73 of file JMutex.hh.

74  {
75  unlock();
76  }
int unlock()
Unlock the mutex.
Definition: JMutex.hh:83

Member Function Documentation

template<typename Lockable >
JBasicScopedLock JSYNCHRONIZATION::JBasicScopedLock< Lockable >::operator= ( const JBasicScopedLock< Lockable > &  )
private
template<typename Lockable >
int JSYNCHRONIZATION::JBasicScopedLock< Lockable >::unlock ( )
inline

Unlock the mutex.

Returns
0 on success, an error code otherwise. See man 3p pthread_mutex_lock for further info.

Definition at line 83 of file JMutex.hh.

84  {
85  return m_lock->unlock();
86  }

Member Data Documentation

template<typename Lockable >
Lockable* JSYNCHRONIZATION::JBasicScopedLock< Lockable >::m_lock
private

Definition at line 49 of file JMutex.hh.


The documentation for this class was generated from the following file: