core/vpl/vpl_mutex.h
Go to the documentation of this file.
00001 // This is core/vpl/vpl_mutex.h
00002 #ifndef vpl_mutex_h_
00003 #define vpl_mutex_h_
00004 //:
00005 // \file
00006 // \author fsm, Oxford RRG
00007 // \date   08 Dec 2001
00008 //
00009 // \verbatim
00010 //  Modifications
00011 //   08 Dec 2001 first version.
00012 // \endverbatim
00013 
00014 #include <vxl_config.h>
00015 
00016 #if VXL_HAS_PTHREAD_H
00017 # include <pthread.h>
00018 # include <vcl_cerrno.h> // for EBUSY, I think
00019 struct vpl_mutex
00020 {
00021   vpl_mutex() { pthread_mutex_init(&mutex_, 0); }
00022 
00023   void lock() { pthread_mutex_lock(&mutex_); }
00024 
00025   //: returns `true' if lock was acquired.
00026   bool trylock() { return pthread_mutex_trylock(&mutex_) != EBUSY; }
00027 
00028   void unlock() { pthread_mutex_unlock(&mutex_); }
00029 
00030   ~vpl_mutex() { pthread_mutex_destroy(&mutex_); }
00031 
00032  private:
00033   pthread_mutex_t mutex_;
00034 
00035   // disallow assignment.
00036   vpl_mutex(vpl_mutex const &) { }
00037   vpl_mutex& operator=(vpl_mutex const &) { return *this; }
00038 };
00039 
00040 #else
00041 # error "only works with pthreads for now"
00042 #endif
00043 
00044 #endif // vpl_mutex_h_