comparison 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
comparison
equal deleted inserted replaced
3479:9b93ec2c53b2 3480:10fd1b9ae044
39 namespace Orthanc 39 namespace Orthanc
40 { 40 {
41 class Semaphore : public boost::noncopyable 41 class Semaphore : public boost::noncopyable
42 { 42 {
43 private: 43 private:
44 unsigned int count_; 44 unsigned int availableResources_;
45 boost::mutex mutex_; 45 boost::mutex mutex_;
46 boost::condition_variable condition_; 46 boost::condition_variable condition_;
47 47
48 void Release(); 48 void Release();
49 49
50 void Acquire(); 50 void Acquire();
51 51
52 bool TryAcquire();
52 public: 53 public:
53 explicit Semaphore(unsigned int count); 54 explicit Semaphore(unsigned int availableResources);
54 55
55 class Locker : public boost::noncopyable 56 class Locker : public boost::noncopyable
56 { 57 {
57 private: 58 private:
58 Semaphore& that_; 59 Semaphore& that_;
67 ~Locker() 68 ~Locker()
68 { 69 {
69 that_.Release(); 70 that_.Release();
70 } 71 }
71 }; 72 };
73
74 class TryLocker : public boost::noncopyable
75 {
76 private:
77 Semaphore& that_;
78 bool isAcquired_;
79
80 public:
81 explicit TryLocker(Semaphore& that) :
82 that_(that)
83 {
84 isAcquired_ = that_.TryAcquire();
85 }
86
87 ~TryLocker()
88 {
89 if (isAcquired_)
90 {
91 that_.Release();
92 }
93 }
94
95 bool IsAcquired() const
96 {
97 return isAcquired_;
98 }
99 };
100
72 }; 101 };
73 } 102 }