Qt Utilities 6.14.4
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
trylocker.h
Go to the documentation of this file.
1#ifndef THREADING_UTILS_TRYLOCKER_H
2#define THREADING_UTILS_TRYLOCKER_H
3
4#include <QtGlobal>
5
6QT_FORWARD_DECLARE_CLASS(QMutex)
7
8namespace QtUtilities {
9
13template <typename Mutex = QMutex> class TryLocker {
14public:
18 TryLocker(Mutex &mutex)
19 : m_mutex(mutex.tryLock() ? &mutex : nullptr)
20 {
21 }
22
28 {
29 if (m_mutex) {
30 m_mutex->unlock();
31 }
32 }
33
37 bool isLocked() const
38 {
39 return m_mutex != nullptr;
40 }
41
45 operator bool() const
46 {
47 return m_mutex != nullptr;
48 }
49
50private:
51 Mutex *m_mutex;
52};
53} // namespace QtUtilities
54
55#endif // THREADING_UTILS_TRYLOCKER_H
Like QMutexLocker, but it just tries to lock the mutex.
Definition trylocker.h:13
bool isLocked() const
Returns whether the mutex could be locked.
Definition trylocker.h:37
~TryLocker()
Unlocks the mutex specified when constructing.
Definition trylocker.h:27
TryLocker(Mutex &mutex)
Tries to lock the specified mutex.
Definition trylocker.h:18