## wxSemaphore

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, 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 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::WaitTimeout

### wxSemaphore::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 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::~wxSemaphore

~wxSemaphore()

Destructor is not virtual, don't use this class polymorphically.

### wxSemaphore::Post

wxSemaError 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.

Return value

One of:

wxSEMA_NO_ERROR | There was no error.
wxSEMA_INVALID | Semaphore hasn't been initialized successfully.
wxSEMA_OVERFLOW | Post() would increase counter past the max.
wxSEMA_MISC_ERROR | Miscellaneous error.

### wxSemaphore::TryWait

wxSemaError TryWait()

Same as Wait(), but returns immediately.

Return value

One of:

wxSEMA_NO_ERROR | There was no error.
wxSEMA_INVALID | Semaphore hasn't been initialized successfully.
wxSEMA_BUSY | Returned by TryWait() if Wait() would block, i.e. the count is zero.
wxSEMA_MISC_ERROR | Miscellaneous error.

### wxSemaphore::Wait

wxSemaError Wait()

Wait indefinitely until the semaphore count becomes strictly positive and then decrement it and return.

Return value

One of:

wxSEMA_NO_ERROR | There was no error.
wxSEMA_INVALID | Semaphore hasn't been initialized successfully.
wxSEMA_MISC_ERROR | Miscellaneous error.

### wxSemaphore::WaitTimeout

wxSemaError WaitTimeout(unsigned longtimeout_millis)

Same as Wait(), but with a timeout limit.

Return value

One of:

wxSEMA_NO_ERROR | There was no error.
wxSEMA_INVALID | Semaphore hasn't been initialized successfully.
wxSEMA_TIMEOUT | Timeout occurred without receiving semaphore.
wxSEMA_MISC_ERROR | Miscellaneous error.