dodo  0.0.1
A C++ library to create containerized Linux services
mutex.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file mutex.hpp
20  * Defines the dodo::threads::Mutex and dodo::threads::Mutexer classes.
21  */
22 
23 #ifndef threads_mutex_hpp
24 #define threads_mutex_hpp
25 
26 #include <mutex>
27 #include <string>
28 
29 namespace dodo::threads {
30 
31  /**
32  * A Mutex to synchronize access to resources between threads.
33  *
34  * Use a Mutexer to control a Mutex by mere scope.
35  *
36  * @see Mutexer
37  * @see std::mutex
38  */
39  class Mutex {
40  public:
41 
42  /**
43  * Construct a Mutex.
44  */
45  Mutex() : mutex_() {};
46 
47  /**
48  * Destruct a Mutex.
49  */
50  virtual ~Mutex() {};
51 
52  /**
53  * Waits for a Mutex and locks it (atomically). You must not call lock if the lock is already
54  * held by the calling thread, that is a deadlock.
55  * @see std::mutex::lock()
56  */
57  void lock() { mutex_.lock(); };
58 
59  /**
60  * If the Mutex is currently not locked, lock it and return true. If the Mutex is locked, return false.
61  * You must not call lock if the lock is alread held by the calling thread, that is a deadlock.
62  * Mutex from the same thread twice - that is a deadlock.
63  * @return true if the Mutex was locked. false if the Mutex was already locked.
64  * @see std::mutex::try_lock()
65  */
66  bool tryLock() { return mutex_.try_lock(); };
67 
68  /**
69  * Unlocks the Mutex. Calling unLOck whilst the calling thread is not holding the lock causes undefined
70  * behavior in the Mutex.
71  * @see std::mutex::unlock()
72  */
73  void unLock() { mutex_.unlock(); };
74 
75  private:
76  /**
77  * the internal std::mutex
78  */
79  std::mutex mutex_;
80  };
81 
82  /**
83  * Waits for and locks the Mutex on construction, unlocks the Mutex when this Mutexer is destructed.
84 
85  * In the below code, it would be safe to call addInt from multiple threads. The Mutexer object calls
86  * thelist_mutex.lock() when constructed, and thelist_mutex.unLock() when it goes out of
87  * scope( the function returns or an exception is thrown).
88  * @code
89  * // Mutex to protect thelist
90  * threads::Mutex thelist_mutex;
91  * std::list<int> thelist;
92  *
93  * function addInt( int i ) {
94  * Mutexer lock( thelist_mutex );
95  * thelist.push_back( i );
96  * }
97  * @endcode
98  * @see Mutex
99  */
100  class Mutexer {
101  public:
102  /**
103  * Constructor.
104  * @param mutex The mutex to guard.
105  */
106  Mutexer( Mutex& mutex ) : mutex_(mutex) { mutex_.lock(); };
107 
108  /**
109  * Destructor.
110  * Unlocks the guarded mutex.
111  */
113 
114  private:
115 
116  /**
117  * Reference to the guarded mutex.
118  */
119  Mutex &mutex_;
120  };
121 
122 }
123 
124 #endif
dodo::threads::Mutexer
Waits for and locks the Mutex on construction, unlocks the Mutex when this Mutexer is destructed.
Definition: mutex.hpp:100
dodo::threads::Mutex::~Mutex
virtual ~Mutex()
Destruct a Mutex.
Definition: mutex.hpp:50
dodo::threads::Mutex::Mutex
Mutex()
Construct a Mutex.
Definition: mutex.hpp:45
dodo::threads::Mutex::mutex_
std::mutex mutex_
the internal std::mutex
Definition: mutex.hpp:73
dodo::threads::Mutex::unLock
void unLock()
Unlocks the Mutex.
Definition: mutex.hpp:73
dodo::threads::Mutex::lock
void lock()
Waits for a Mutex and locks it (atomically).
Definition: mutex.hpp:57
dodo::threads::Mutex
A Mutex to synchronize access to resources between threads.
Definition: mutex.hpp:39
dodo::threads::Mutexer::mutex_
Mutex & mutex_
Reference to the guarded mutex.
Definition: mutex.hpp:112
dodo::threads::Mutex::tryLock
bool tryLock()
If the Mutex is currently not locked, lock it and return true.
Definition: mutex.hpp:66
dodo::threads::Mutexer::Mutexer
Mutexer(Mutex &mutex)
Constructor.
Definition: mutex.hpp:106
dodo::threads::Mutexer::~Mutexer
~Mutexer()
Destructor.
Definition: mutex.hpp:112
dodo::threads
Interface for Thread programming.
Definition: mutex.hpp:29