cpp-pthread  (v1.7.3)
Simple C++ wrapper to pthread functions.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
mutex.cpp
1 //
2 // mutex.cpp
3 // cpp-pthread
4 //
5 // Created by herbert koelman on 18/03/2016.
6 //
7 //
8 
9 #include "pthread/mutex.hpp"
10 
11 namespace pthread {
12 
13  mutex::mutex () {
14  auto rc = pthread_mutex_init (&_mutex, NULL);
15  if ( rc != 0 ) {
16  throw mutex_exception ( "In constructor of mutex pthread_mutex_init(&mutex, NULL) failed. ", rc );
17  }
18  }
19 
20  mutex::~mutex () {
21  pthread_mutex_destroy (&_mutex);
22  }
23 
24  void mutex::lock () {
25  int rc = -1;
26  rc = pthread_mutex_lock ( &_mutex );
27  if ( rc != 0 ){
28  throw mutex_exception("pthread_mutex_lock failed.", rc);
29  }
30  }
31 
32  void mutex::try_lock () {
33 
34  auto rc = pthread_mutex_trylock ( &_mutex );
35  if ( rc != 0 ){
36  throw mutex_exception("pthread_mutex_trylock failed, already locked.", rc);
37  }
38  }
39 
40  void mutex::unlock () {
41  auto rc = pthread_mutex_unlock ( &_mutex );
42  if ( rc != 0 ){
43  throw mutex_exception("pthread_mutex_unlock failed.", rc);
44  }
45  }
46 
47 }
void unlock()
Definition: mutex.cpp:40
pthread_mutex_t _mutex
Definition: mutex.hpp:74
void lock()
Definition: mutex.cpp:24
void try_lock()
Definition: mutex.cpp:32