Overte C++ Documentation
TryLocker.h
1 //
2 // TryLocker.h
3 // libraries/shared/src
4 //
5 // Created by Brad Davis on 2015/03/16.
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_TryLocker_h
13 #define hifi_TryLocker_h
14 
15 #include <QMutex>
16 
17 class MutexTryLocker {
18  QMutex& _mutex;
19  bool _locked{ false };
20 public:
21  MutexTryLocker(QMutex &m) : _mutex(m), _locked(m.tryLock()) {}
22  ~MutexTryLocker() { if (_locked) _mutex.unlock(); }
23  bool isLocked() {
24  return _locked;
25  }
26 };
27 
28 #endif // hifi_TryLocker_h