Re: Thread Checking the Queue data in an infinite loop

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



This is completely wrong. And an Event Object is completely wrong. An AutoReset event is
also wrong. See my essay on the use of sempahores. A Semaphore is the only correct
synchronization object to use to tell if something is in a queue. Also, you do not need a
mutex to write to the queue, use a CRITICAL_SECTION instead, it is vastly more efficient.
You also need to do WFMO so you can shut the thread down, also in my essay on semaphores.

Your situation is correct, your use of mutex is a poor choice and the use of an event is
simply incorrect.

http://www.flounder.com/semaphores.htm

joe

On Fri, 11 Jan 2008 04:03:03 -0800 (PST), prams <pramodjoisb@xxxxxxxxx> wrote:

Hello,

I have a Thread which runs in a while(1) infinite loop checking for
any data in the Queue(normal STL::Queue) and if any data is there, it
retrieves and does some functionality with it. Now since it is an
infinite loop, continousy checking for data, CPU Utilization is almost
95-100% for my process. To Avoid that, I have tempororily put
Sleep(1). But I know this is not a good solution.

I want to replace it with this Solution. Please let me know if this is
correct.

1) I will create a Synchronization Object i.e an Event Object which
is an Autoreset Event Object.

2) The threads which write the data to the Queue (There are 2 threads
writing data to the queue simoultaneously..Queue writing is
synchronized with a mutex) will set the Event Object (Set the state to
signalled)

3) The Thread which reads the Queue data will do a WaitForSingleObject
waiting for the Event Object to be signalled. So it will be in a idle
state (waiting) if there is no data in the queue.
So I can avoid CPU Utilization(100%) problem.

Please let me know whether my solution is proper or should I have to
use seperate Event Objects.?

Rgds/;Pramod
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Thread Checking the Queue data in an infinite loop
    ... is an Autoreset Event Object. ... The threads which write the data to the Queue (There are 2 threads ... have to acquire the mutex. ... The reader would have to drain the queue each time it gets control. ...
    (microsoft.public.vc.mfc)
  • Re: Thread Checking the Queue data in an infinite loop
    ... regarding my Query. ... Others who commented on My solution using Event Object: ... Yes I want to drain the Queue once I compelete the wait..Since the ... Please let me know the proper way to solve this. ...
    (microsoft.public.vc.mfc)
  • Re: Thread Checking the Queue data in an infinite loop
    ... is an Autoreset Event Object. ... The threads which write the data to the Queue (There are 2 threads ... The Thread which reads the Queue data will do a WaitForSingleObject ... state (waiting) if there is no data in the queue. ...
    (microsoft.public.vc.mfc)
  • Thread Checking the Queue data in an infinite loop
    ... infinite loop, continousy checking for data, CPU Utilization is almost ... is an Autoreset Event Object. ... The threads which write the data to the Queue (There are 2 threads ... state (waiting) if there is no data in the queue. ...
    (microsoft.public.vc.mfc)