Apparently someone at Microsoft decided that a ReleaseSemaphore with
a count of zero is illegal, so it can't be used to get the count
value as a "previous" value. Is there a way to get the semaphore
count without affecting the signaling of the semaphore?
No.
And it's a good thing, too, because you could never rely on the
answer being correct (or even close), since another thread (or two,
or ten) could change it between when the function returns and when
your code examines the return value.
There could be many reasons for monitoring the count, but that wasn't
the point. ReleaseSemaphore returns the "previous count" as an
optional output parameter, so apparently someone at Microsoft thought
it was important enough to make the count accessable. So why make a
single exception to this and make a call with a release count of zero
illegal instead of just returning the "previous count"?
One of the reasons for monitoring the count is that sometimes the
behavior of an application can be altered if the count exceeds some
threshold. For example, if the count is being used as a pending message
counter
for a thread, then if the message count exceeds some threshold, the
message receiving thread could speeds up it's message processing by
altering it's behavior (a graphics thread reducing the quality and
increasing the speed of it's output).
be much better for anything like that.