Re: Is there a way to get the current semaphore count?
- From: "Ben Voigt [C++ MVP]" <bvoigt@xxxxxxxxxxxxxxxx>
- Date: Thu, 2 Jul 2009 13:17:04 -0500
jeffareid wrote:
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).
Why would you use a semaphore to count messages? The Interlocked API would
be much better for anything like that.
.
- Follow-Ups:
- Re: Is there a way to get the current semaphore count?
- From: jeffareid
- Re: Is there a way to get the current semaphore count?
- References:
- Is there a way to get the current semaphore count?
- From: jeffareid
- Re: Is there a way to get the current semaphore count?
- From: Carl Daniel [VC++ MVP]
- Re: Is there a way to get the current semaphore count?
- From: jeffareid
- Is there a way to get the current semaphore count?
- Prev by Date: Re: Window won't update
- Next by Date: Re: class nested in template class: Dev-C works while VC6 not
- Previous by thread: Re: Is there a way to get the current semaphore count?
- Next by thread: Re: Is there a way to get the current semaphore count?
- Index(es):
Relevant Pages
|