Image Component Library (ICL)
Lockable.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/Lockable.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Mutex.h>
36 
37 namespace icl{
38  namespace utils{
39 
41  class Lockable{
42 
45 
46  public:
47 
49  Lockable(bool recursive=false):
50  m_mutex(new Mutex(recursive ? Mutex::mutexTypeRecursive : Mutex::mutexTypeNormal)){
51  }
52 
54  Lockable(const Lockable &l) : m_mutex(new Mutex(l.m_mutex->type)){
55 
56  }
57 
60  delete m_mutex;
61  m_mutex = new Mutex(l.m_mutex->type);
62  return *this;
63  }
64 
67  delete m_mutex;
68  }
69 
71  void lock() const { m_mutex->lock(); }
72 
74  void unlock() const { m_mutex->unlock(); }
75 
77  Mutex &getMutex() const { return *m_mutex; }
78  };
79 
80  } // namespace utils
81 }
82 
undocument this line if you encounter any issues!
Definition: Any.h:37
Lockable(const Lockable &l)
copy constructor (does not copy the source mutex)
Definition: Lockable.h:54
void unlock()
unlocks the mutex
Definition: Mutex.h:111
~Lockable()
Destructor.
Definition: Lockable.h:66
Lockable(bool recursive=false)
Default constructor.
Definition: Lockable.h:49
void lock() const
lock object
Definition: Lockable.h:71
Mutex & getMutex() const
returns mutex of this object
Definition: Lockable.h:77
enum icl::utils::Mutex::MutexType type
void unlock() const
unlock object
Definition: Lockable.h:74
Interface for objects, that can be locked using an internal mutex.
Definition: Lockable.h:41
Lockable & operator=(const Lockable &l)
assignment operator (does not copy the source mutex)
Definition: Lockable.h:59
Mutex class of the ICL.
Definition: Mutex.h:54
Mutex * m_mutex
wrapped mutex variable
Definition: Lockable.h:44
void lock()
locks the mutex
Definition: Mutex.h:91