TDME2  1.9.200
Mutex.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <tdme/tdme.h>
6 
7 #include <mutex>
8 #include <string>
9 
10 using std::mutex;
11 using std::string;
12 
13 /**
14  * Mutex implementation.
15  * Mutexes are used to ensure that only one process can run a critical section,
16  * which is e.g. modifying shared data between thread.
17  * @author Andreas Drewke
18  */
20  friend class Condition;
21 
22 public:
23  // forbid class copy
25 
26  /**
27  * @brief Public constructor
28  * @param name name
29  */
30  inline Mutex(const string& name): name(name) {};
31 
32  /**
33  * @brief Destroys the mutex
34  */
35  inline ~Mutex() {}
36 
37  /**
38  * @brief Tries to locks the mutex
39  */
40  inline bool tryLock() {
41  return stlMutex.try_lock();
42  }
43 
44  /**
45  * @brief Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
46  */
47  inline void lock() {
48  stlMutex.lock();
49  }
50 
51  /**
52  * @brief Unlocks this mutex
53  */
54  inline void unlock() {
55  stlMutex.unlock();
56  }
57 
58 private:
59  string name;
60  mutex stlMutex;
61 };
Threading condition variable implementation.
Definition: Condition.h:23
Mutex implementation.
Definition: Mutex.h:19
~Mutex()
Destroys the mutex.
Definition: Mutex.h:35
void unlock()
Unlocks this mutex.
Definition: Mutex.h:54
void lock()
Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
Definition: Mutex.h:47
bool tryLock()
Tries to locks the mutex.
Definition: Mutex.h:40
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6