Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TStaticSync.h
Go to the documentation of this file.
1#pragma once
2
4#include <misc/gen/Sync.h>
5#include <misc/global.h>
6
7namespace sf
8{
9
57template<typename T>
59{
60 public:
65
66 protected:
71
76
81
85 class Lock : public Mutex::Lock
86 {
87 public:
92 : Mutex::Lock(*TStaticSync<T>::_mutex, false)
93 {}
94
100 explicit Lock(bool try_lock)
101 : Mutex::Lock(*TStaticSync<T>::_mutex, try_lock)
102 {}
103 };
104
105 private:
109 static Mutex* _mutex;
113 static unsigned _count;
114};
115
116template<typename T>
118{
125 if (_count++ == 0)
126 {
127 _mutex = new Mutex;
128 }
129}
130
131template<typename T>
133{
134 _count++;
135}
136
137template<typename T>
139{
140 // If this is the only remaining TStaticSync<T> object, destroy the semaphore.
141 if (--_count == 0)
142 {
143 delete _mutex;
144 }
145}
146
147}// namespace sf
Locking class for easy locking and unlocking by destructor.
Definition Mutex.h:43
Lightweight intra process thread synchronization.
Definition Mutex.h:17
Definition TStaticSync.h:86
Lock(bool try_lock)
Try constructor.
Definition TStaticSync.h:100
Lock()
Default constructor.
Definition TStaticSync.h:91
TStaticSync provides a system-independent interface to build sets of classes that act somewhat like m...
Definition TStaticSync.h:59
TStaticSync()
Default constructor.
Definition TStaticSync.h:117
~TStaticSync()
Destructor.
Definition TStaticSync.h:138
TStaticSync< T > & operator=(const TStaticSync< T > &)=delete
Do not allow copying.
Definition Application.h:10