Qt Utilities: misc/trylocker.h Source File

Qt Utilities 6.21.2 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
13 template <typename Mutex = QMutex> class TryLocker {
14public:
18 TryLocker(Mutex &mutex)
19 : m_mutex(mutex.tryLock() ? &mutex : nullptr)
20 {
21 }
22
27 ~TryLocker()
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
QtUtilities::TryLocker::isLocked
bool isLocked() const
Returns whether the mutex could be locked.
Definition trylocker.h:37
QtUtilities::TryLocker::~TryLocker
~TryLocker()
Unlocks the mutex specified when constructing.
Definition trylocker.h:27
QtUtilities::TryLocker::TryLocker
TryLocker(Mutex &mutex)
Tries to lock the specified mutex.
Definition trylocker.h:18
QtUtilities
!
Definition trylocker.h:8