#include <Synchronization.h>
Public Methods | |
| Semaphore (int count) | |
| Create a semaphore with a given initial value. More... | |
| Semaphore () | |
| Create a semaphore with an initial value of 0. More... | |
| void | Wait () |
| Wait functions will return successfully when the count is greater than one. More... | |
| bool | TryWait () |
| Non-blocking Wait on semaphore. More... | |
| bool | Wait (int millisecondTimeout) |
| Blocking wait on semaphore with timeout. More... | |
| void | Signal () |
| Signal the semaphore. More... | |
Private Attributes | |
| Mutex | m_mutex |
| Mutex used internally to maintain thread safety. More... | |
| int | m_count |
| The semaphore's value. More... | |
This is a general-purpose cross-platform counting semaphore.
Definition at line 103 of file Synchronization.h.
|
|
Create a semaphore with a given initial value. When the count is greater than 0 a Wait call will succeed.
Definition at line 121 of file Synchronization.cpp.
00121 : m_count(count) {} |
|
|
Create a semaphore with an initial value of 0. The semaphore must be signalled at least once before a wait call will be successful. Definition at line 122 of file Synchronization.cpp.
00122 : m_count(0) {} |
|
|
Signal the semaphore. Increment the semaphore. When it becomes > 0, if a thread is waiting on it, that thread will be released. Definition at line 192 of file Synchronization.cpp. References m_count, and m_mutex.
|
|
|
Non-blocking Wait on semaphore. The semaphore will check to see if its value is > 0. If so, it will be decremented and the semaphore will return true.
Definition at line 144 of file Synchronization.cpp. References m_count, and m_mutex.
|
|
|
Blocking wait on semaphore with timeout. This semaphore will block on its value for at least millisecondTimeout milliseconds or until the value of semaphore becomes > 0. If it becomes > 0 the semaphore will be decremented and the semaphore will return true.
Definition at line 156 of file Synchronization.cpp. References m_count, and m_mutex.
00157 {
00158 #ifdef _WIN32
00159 int timeout = GetTickCount() + millisecondTimeout;
00160 #endif
00161 #ifdef LINUX
00162 timeb timeout;
00163 ftime(&timeout);
00164 int tempMilli = (int)timeout.millitm + millisecondTimeout;
00165 timeout.time = timeout.time + tempMilli/1000;
00166 timeout.millitm = tempMilli /1000;
00167 #endif
00168 m_mutex.Wait();
00169 while(m_count == 0)
00170 {
00171 m_mutex.Release();
00172 #ifdef _WIN32
00173 Sleep(0);
00174 if(GetTickCount() > timeout) return false;
00175 #endif
00176 #ifdef LINUX
00177 sched_yield();
00178 timeb curtime;
00179 ftime(&curtime);
00180 if((timeout.time <= curtime.time) && (timeout.millitm <= curtime.millitm))
00181 {
00182 return false;
00183 }
00184 #endif
00185 m_mutex.Wait();
00186 }
00187 m_count--;
00188 m_mutex.Release();
00189 return true;
00190 }
|
|
|
Wait functions will return successfully when the count is greater than one. The semaphore will block until its value is > 0. Then the semaphore value will be decremented and the function will return. Definition at line 126 of file Synchronization.cpp. References m_count, and m_mutex.
|
|
|
The semaphore's value.
Definition at line 153 of file Synchronization.h. |
|
|
Mutex used internally to maintain thread safety.
Definition at line 151 of file Synchronization.h. |
1.2.12 written by Dimitri van Heesch,
© 1997-2001