wxSemaphore is a counter limiting the number of threads concurrently accessing a shared resource. This counter is always between 0 and the maximum value specified during the semaphore creation. When the counter is strictly greater than 0, a call to Wait returns immediately and decrements the counter. As soon as it reaches 0, any subsequent calls to Wait block and only return when the semaphore counter becomes strictly positive again as the result of calling Post which increments the counter.
In general, the semaphores are useful to restrict access to a shared resource which can only be accessed by some fixed number of clients at once. For example, when modeling a hotel reservation system a semaphore with the counter equal to the total number of available rooms could be created. Each time a room is reserved, the semaphore should be acquired by calling Wait and each time a room is freed it should be released by calling Post.
Derived from
No base class
Include files
<wx/thread.h>
Members
wxSemaphore::wxSemaphore
wxSemaphore::~wxSemaphore
wxSemaphore::Post
wxSemaphore::TryWait
wxSemaphore::Wait
wxSemaphore(int initialcount = 0, int maxcount = 0)
Specifying a maxcount of 0 actually makes wxSemaphore behave as if there is no upper limit. If maxcount is 1 the semaphore behaves exactly as a mutex.
initialcount is the initial value of the semaphore which must be between 0 and maxcount (if it is not set to 0).
~wxSemaphore()
Destructor is not virtual, don't use this class polymorphically.
void Post()
Increments the semaphore count and signals one of the waiting threads in an atomic way.
bool TryWait()
Same as Wait(), but does not block, returns TRUE if the semaphore was successfully acquired and FALSE if the count is zero and it couldn't be done.
void Wait()
Wait indefinitely until the semaphore count becomes strictly positive and then decrement it and return.
bool Wait(unsigned long timeout_millis)
Same as the version above, but with a timeout limit: returns TRUE if the semaphore was acquired and FALSE if the timeout has elapsed