Re: Is there a way to get the current semaphore count?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance






"jeffareid" <jeffareid@xxxxxxxxxxx> wrote in message news:U493m.22480$S16.13826@xxxxxxxxxxxxxxx
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?

ReleaseSemaphore returns the "previous count" as an optional output
parameter.

Why would you use a semaphore to count messages? The Interlocked API
would be much better for anything like that.

Again not the point. ReleaseSemaphore already returns the "previous count"
as an optional output parameter, so why make a release count of zero call
illegal instead of allowing it to return the current count?

A semaphore used to represent the count of messages in a fifo message
queue, combined with a mutex for ownership of that queue during updates,
can take advantage of WaitForMultipleObjects(), which eliminates priority
and multi-event conflicts. The combination is idea for inter-thread
fifo messaging.

Or you could just use MsgWaitForMultipleObjects and a real message to wake the thread... with Interlocked* to maintain a count of queued messages if you want.

But unless I'm much mistaken, semaphores start at N and count down, while message counts start at 0 and count up, and the bound is potentially very high.

Semaphores are designed for things like sharing a pool of M database connections between N threads.



Below is a link to example multi-threaded code for a file copy. I use it
as a template for multi-threaded apps. One thread reads from a source
file, the other thread writes to a destination file, and each thread has
an input fifo queue of messages with pointers to buffers and buffer size:

http://jeffareid.net/misc/mtcopy.zip

There is some setup overhead, but look how simple the code in
ThreadFile0() and ThreadFile1() is.

Haven't looked at your example yet because it requires a download, etc. But from the description it sounds like you could do very well with just an event.

In such cases I wouldn't even use multiple threads. You don't have multiple computations that need to run at once. I/O completion routines are the way to go, but OVERLAPPED I/O and events work pretty well too. Or IOCP, which I've never bothered to figure out because completion routines are so straightforward.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4212 (20090703) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



.



Relevant Pages

  • Re: Is there a way to get the current semaphore count?
    ... so why make a release count of zero call ... A semaphore used to represent the count of messages in a fifo message ... queue, combined with a mutex for ownership of that queue during updates, ... and multi-event conflicts. ...
    (microsoft.public.vc.language)
  • Re: How to get synchronized efficiently
    ... A semaphore, behaving as a mutex, is one way to protect access to a queue. ... The choice of the best sync model ALWAYS depends on the design of your ... Let's set aside IOCP and consider the semaphore case. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Multithreaded queue with wait event
    ... if two threads release the mutex and are just about to ... See, if that happens, one thread will receive the event and dequeue. ... your queue can indeed return no item when there is no timeout ... I have two implementations, one using the semaphore, the other using ...
    (comp.programming.threads)
  • Re: Multithreaded queue with wait event
    ... I have two implementations, one using the semaphore, the other using ...   if queue size> semaphore max, release mutex, return failure ... is also a simple boolean "shutdown" flag, used to prevent too many new ...
    (comp.programming.threads)
  • Re: Explain this about threads
    ... There is usually no reason to sit and spin in the meantime, so normally the remainder of B's time slice would be yielded to the scheduler in hopes that some other task has a use for the CPU resource. ... A semaphore can be viewed as a data structure which represents a resource or event. ... One key aspect of a classic OS semaphore structure is a queue to which tasks awaiting the resource or event can chain their task control blocks. ... But this requires a task switch somewhere to go from user mode to kernel mode. ...
    (microsoft.public.dotnet.languages.csharp)