
Anthony Williams wrote:
Unfortunately, it is susceptible to the "stolen wakeup" problem. If m_waitersCount is non-zero, the semaphore is incremented on line D. This will wake the appropriate number of waiting threads, but not immediately. Threads waiting on a semaphore will not be woken until they are next scheduled. Thus, a new thread might call wait, increment the waitersCount (line A) and acquire the (line B) before all the threads currently waiting have been woken.
You seem to be implying that ReleaseSemaphore with a count > 1 is not an atomic operation but behaves as a series of ReleaseSemaphore with count == 1 calls? Is that correct? Or did I misunderstand something? I'd expect it to atomically wake count threads and decrement the semaphore counter with count.