Mercurial > hg > orthanc
diff Core/MultiThreading/Semaphore.h @ 3480:10fd1b9ae044
fix Semaphore + added TryAcquire & TryLocker
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 01 Aug 2019 18:00:22 +0200 |
parents | 4e43e67f8ecf |
children | 4d26261fd05a |
line wrap: on
line diff
--- a/Core/MultiThreading/Semaphore.h Thu Aug 01 11:46:24 2019 +0200 +++ b/Core/MultiThreading/Semaphore.h Thu Aug 01 18:00:22 2019 +0200 @@ -41,7 +41,7 @@ class Semaphore : public boost::noncopyable { private: - unsigned int count_; + unsigned int availableResources_; boost::mutex mutex_; boost::condition_variable condition_; @@ -49,8 +49,9 @@ void Acquire(); + bool TryAcquire(); public: - explicit Semaphore(unsigned int count); + explicit Semaphore(unsigned int availableResources); class Locker : public boost::noncopyable { @@ -69,5 +70,33 @@ that_.Release(); } }; + + class TryLocker : public boost::noncopyable + { + private: + Semaphore& that_; + bool isAcquired_; + + public: + explicit TryLocker(Semaphore& that) : + that_(that) + { + isAcquired_ = that_.TryAcquire(); + } + + ~TryLocker() + { + if (isAcquired_) + { + that_.Release(); + } + } + + bool IsAcquired() const + { + return isAcquired_; + } + }; + }; }