#include <wx/thread.h>
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 wxSemaphore::Wait() returns immediately and decrements the counter. As soon as it reaches 0, any subsequent calls to wxSemaphore::Wait block and only return when the semaphore counter becomes strictly positive again as the result of calling wxSemaphore::Post which increments the counter.
In general, semaphores are useful to restrict access to a shared resource which can only be accessed by some fixed number of clients at the same time. 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 wxSemaphore::Wait and each time a room is freed it should be released by calling wxSemaphore::Post.
Public Member Functions | |
wxSemaphore (int initialcount=0, int maxcount=0) | |
Specifying a maxcount of 0 actually makes wxSemaphore behave as if there is no upper limit. More... | |
~wxSemaphore () | |
Destructor is not virtual, don't use this class polymorphically. More... | |
wxSemaError | Post () |
Increments the semaphore count and signals one of the waiting threads in an atomic way. More... | |
wxSemaError | TryWait () |
Same as Wait(), but returns immediately. More... | |
wxSemaError | Wait () |
Wait indefinitely until the semaphore count becomes strictly positive and then decrement it and return. More... | |
wxSemaError | WaitTimeout (unsigned long timeout_millis) |
Same as Wait(), but with a timeout limit. More... | |
wxSemaphore::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 almost as a mutex (but unlike a mutex it can be released by a thread different from the one which acquired it).
initialcount is the initial value of the semaphore which must be between 0 and maxcount (if it is not set to 0).
wxSemaphore::~wxSemaphore | ( | ) |
Destructor is not virtual, don't use this class polymorphically.
wxSemaError wxSemaphore::Post | ( | ) |
Increments the semaphore count and signals one of the waiting threads in an atomic way.
Returns wxSEMA_OVERFLOW if the count would increase the counter past the maximum.
wxSemaError wxSemaphore::TryWait | ( | ) |
wxSemaError wxSemaphore::Wait | ( | ) |
Wait indefinitely until the semaphore count becomes strictly positive and then decrement it and return.
wxSemaError wxSemaphore::WaitTimeout | ( | unsigned long | timeout_millis | ) |
Same as Wait(), but with a timeout limit.