cpp-pthread  (v1.7.3)
Simple C++ wrapper to pthread functions.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
read_write_lock.cpp
1 
2 #include "pthread/read_write_lock.hpp"
3 
4 
5 namespace pthread {
6 
7  void write_lock::lock (){
8  int ret = pthread_rwlock_wrlock(&_rwlock);
9  if ( ret != 0 ){
10  throw read_write_lock_exception("Try get write lock failed.", ret);
11  };
12  }
13 
15  int ret = pthread_rwlock_trywrlock(&_rwlock);
16  if ( ret != 0 ){
17  throw read_write_lock_exception("Try get write lock failed.", ret);
18  };
19  }
20 
21  write_lock::write_lock (){//:read_lock(){
22  // intentional
23  // the read/write lock is created by the base class read_lock
24  }
25 
27  // intentional... base class is in charge of freeing allocated ressources
28  }
29 
30  // read_lock -----------------------------
31  //
32  void read_lock::lock (){
33  int ret = pthread_rwlock_rdlock(&_rwlock);
34  if ( ret != 0 ){
35  throw read_write_lock_exception("Try get read lock failed.", ret);
36  }
37  }
38 
40  int ret = pthread_rwlock_tryrdlock(&_rwlock);
41  if ( ret != 0 ){
42  throw read_write_lock_exception("Try get read lock failed.", ret);
43  }
44  }
45 
47  int ret = pthread_rwlock_unlock(&_rwlock);
48  if ( ret != 0 ){
49  throw read_write_lock_exception("failed to unlock read/read lock.", ret);
50  }
51  }
52 
54  int ret = pthread_rwlock_init(&_rwlock, NULL);
55  if ( ret != 0 ){
56  throw read_write_lock_exception("Failed to init read/write lock", ret);
57  }
58  }
59 
61  pthread_rwlock_destroy(&_rwlock);
62  }
63 
64 
66 } // namespace pthread
pthread_rwlock_t _rwlock