Mercurial > hg > orthanc
diff Core/MultiThreading/Semaphore.cpp @ 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 | 94f4a18a79cc |
line wrap: on
line diff
--- a/Core/MultiThreading/Semaphore.cpp Thu Aug 01 11:46:24 2019 +0200 +++ b/Core/MultiThreading/Semaphore.cpp Thu Aug 01 18:00:22 2019 +0200 @@ -39,10 +39,10 @@ namespace Orthanc { - Semaphore::Semaphore(unsigned int count) : - count_(count) + Semaphore::Semaphore(unsigned int availableResources) : + availableResources_(availableResources) { - if (count_ == 0) + if (availableResources_ == 0) { throw OrthancException(ErrorCode_ParameterOutOfRange); } @@ -52,7 +52,7 @@ { boost::mutex::scoped_lock lock(mutex_); - count_++; + availableResources_++; condition_.notify_one(); } @@ -60,11 +60,24 @@ { boost::mutex::scoped_lock lock(mutex_); - while (count_ == 0) + while (availableResources_ == 0) { condition_.wait(lock); } - count_++; + availableResources_--; + } + + bool Semaphore::TryAcquire() + { + boost::mutex::scoped_lock lock(mutex_); + + if (availableResources_ == 0) + { + return false; + } + + availableResources_--; + return true; } }