[Home] [Prev] [Next] [Up]
XGSemaphore class
Provides basic semaphore support. Semaphores are used to protect portions of code which are not reentrant.
Usage:
#include <XThread.h>
class XGSemaphore;
Description
This class encapsulates a global semaphore object which provides serialized access to critical sections of code.
Notes
Normally this object is created as a global object, though it can be created as a stack or pointer object.
Constructor/Destructors
XGSemaphore::XGSemaphore(unsigned long count = 1)
This initializes the semaphore object with the specified count. The 'count' provides the number of allowed accesses to the critical code section protected by this semaphore.
XGSemaphore::~XGSemaphore()
This deletes this semaphore. Caution: deleting this object while another thread is in the critical section protected by this semaphore is bad for your application's long-term health.
Critical section access
void XGSemaphore::EnterCritical(void)
If the internal count of the semaphore is greater than 0, this immediately returns, after decrementing the count. If the internal count is 0, this thread spin-locks until the count goes non-zero.
This is used to provide access to a critical portion of code.
void XGSemaphore::LeaveCritical(void)
This increments the semaphore's internal count.
This is used to indicate the thread has left the critical section of the code.