vpl_mutex.h
Go to the documentation of this file.
1 // This is core/vpl/vpl_mutex.h
2 #ifndef vpl_mutex_h_
3 #define vpl_mutex_h_
4 //:
5 // \file
6 // \author fsm, Oxford RRG
7 // \date 08 Dec 2001
8 //
9 // \verbatim
10 // Modifications
11 // 08 Dec 2001 first version.
12 // \endverbatim
13 
14 #include <cerrno>
15 #include "vxl_config.h"
16 #include "vpl/vpl_export.h"
17 
18 #if VXL_HAS_PTHREAD_H
19 # include <pthread.h>
20 struct VPL_EXPORT vpl_mutex
21 {
22  vpl_mutex() { pthread_mutex_init(&mutex_, nullptr); }
23 
24  void lock() { pthread_mutex_lock(&mutex_); }
25 
26  //: returns `true' if lock was acquired.
27  bool trylock() { return pthread_mutex_trylock(&mutex_) != EBUSY; }
28 
29  void unlock() { pthread_mutex_unlock(&mutex_); }
30 
31  ~vpl_mutex() { pthread_mutex_destroy(&mutex_); }
32 
33  private:
34  pthread_mutex_t mutex_;
35 
36  // disallow assignment.
37  vpl_mutex(vpl_mutex const &) { }
38  vpl_mutex& operator=(vpl_mutex const &) { return *this; }
39 };
40 
41 #else
42 # error "only works with pthreads for now"
43 #endif
44 
45 #endif // vpl_mutex_h_