Visual Servoing Platform  version 3.0.0
vpMutex.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Mutex protection.
32  *
33  * Authors:
34  * Celine Teuliere
35  *
36  *****************************************************************************/
37 
38 
39 #ifndef __VP_MUTEX__
40 #define __VP_MUTEX__
41 
42 #include <visp3/core/vpConfig.h>
43 #include <visp3/core/vpException.h>
44 #ifdef VISP_HAVE_PTHREAD
45 
46 #include <pthread.h>
47 
58 class VISP_EXPORT vpMutex {
59 public:
60  vpMutex() : m_mutex() {
61  pthread_mutex_init( &m_mutex, NULL );
62  }
63  void lock() {
64  pthread_mutex_lock( &m_mutex );
65  }
66  void unlock() {
67  pthread_mutex_unlock( &m_mutex );
68  }
69 
80  class VISP_EXPORT vpScopedLock
81  {
82  private:
83  vpMutex & _mutex;
84 
85 // private:
86 //#ifndef DOXYGEN_SHOULD_SKIP_THIS
87 // vpScopedLock &operator=(const vpScopedLock &){
88 // throw vpException(vpException::functionNotImplementedError,"Not implemented!");
89 // return *this;
90 // }
91 //#endif
92 
93  public:
95  : _mutex(mutex)
96  {
97  _mutex.lock();
98  }
100  {
101  _mutex.unlock();
102  }
103  };
104 private:
105  pthread_mutex_t m_mutex;
106 };
107 
108 #endif
109 
110 #endif
Class that allows protection by mutex.
Definition: vpMutex.h:80
void lock()
Definition: vpMutex.h:63
void unlock()
Definition: vpMutex.h:66
Class that allows protection by mutex.
Definition: vpMutex.h:58
vpMutex()
Definition: vpMutex.h:60
vpScopedLock(vpMutex &mutex)
Definition: vpMutex.h:94