threading waiting on message queues

M

Mike

Hello,

Here is my challenge.

I have a thread awaiting events being sent via Queue. Currently I am
checking the count and if there is a count, Dequeue() the next object
and process it. By design, however, this is inefficient usage of CPU
clocks.

A more efficient usage could be to wrap Monitor::Wait() and
Monitor::pulse() events. If Monitor operations like I think it does, I
should be able to Wait() efficiently forever for the next event to
arrive and before I Dequeue() it. And use Pulse() to signal when a
next event has been Enqueue()d.

Does Wait() wait efficiently for the next pulse to arrive?
Are pulses queued cumulatively?
Or is there only one state (pulsed or not)?
(Not insurmountable if this cannot happen.)

Best regards,
Michael Powell
 
C

Carl Daniel [VC++ MVP]

Mike said:
Hello,

Here is my challenge.

I have a thread awaiting events being sent via Queue. Currently I am
checking the count and if there is a count, Dequeue() the next object
and process it. By design, however, this is inefficient usage of CPU
clocks.

A more efficient usage could be to wrap Monitor::Wait() and
Monitor::pulse() events. If Monitor operations like I think it does, I
should be able to Wait() efficiently forever for the next event to
arrive and before I Dequeue() it. And use Pulse() to signal when a
next event has been Enqueue()d.

Does Wait() wait efficiently for the next pulse to arrive?
Yes.

Are pulses queued cumulatively?
No.

Or is there only one state (pulsed or not)?

Yes. The states are more like owned & unowned but the implication is (I
think) what you're looking for: Monitor::pulse will wake a single waiting
thread a single time.
(Not insurmountable if this cannot happen.)

The docs in MSDN on Montor::Wait, Pulse, PulseAll are pretty thorough - be
sure to start by reading and understanding all of those.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top