spree

Hey everyone :)

I wish to know how can I translate a "busy waiting loop" approach into an event driven approach

Lets say I have a queue of tasks, whenever there are elements in the queue, a process has to treat these elements

Now I do it like this:

while(1){

if(MyQ.Size()>0){

My.Q.RemoveHead ....

}

}

This approach causes the cpu to work on full load, even if there are no elements in the queue.

Is there a way to avoid this loop and somehow to create an event driven approach. Something like OnQueueHasElements...

Thanks in advance :)



Re: Visual C++ Language How to avoid Busy Waiting?

Espen Ruud Schultz

This is more a system specific problem than a language problem. Depends on what system you're on, but all systems have some sort of queue you can utilize.

, Espen





Re: Visual C++ Language How to avoid Busy Waiting?

MNour.AI

Try to use a "Delegate".

Regards.






Re: Visual C++ Language How to avoid Busy Waiting?

spree

Delegate is a pointer to a function right

But who will monitor this evenet something has to check that my MyQueue.Size > 0 and trigger the event





Re: Visual C++ Language How to avoid Busy Waiting?

Espen Ruud Schultz

As I understand it you don't want your infinitive loop to hug all the cpu time. You said this approach "causes the cpu to work on full load". Well, it's impossible to not have the cpu work on full load. It's designed to never stop. So what you have to do is to tell the system, for example Windows or Linux, that your program has nothing to do while it's waiting for something to enter your queue. So you'll free the cpu to do something usefull instead of running an infinitive loop, and useless as long as there's nothing in your queue.

This is system specific and has nothing to do with the language. I suggest that you look up whatever messagequeue the system you're running on utilize. If you're on Windows go search msdn for "GetMessage" to get you started.

, Espen





Re: Visual C++ Language How to avoid Busy Waiting?

spree

Thanks for your advices, I"ll look it up.



Re: Visual C++ Language How to avoid Busy Waiting?

Marius Bancila

You have to give the other threads a chance to run by calling Sleep().

while(1)

{

if(MyQ.Size()>0)

{

My.Q.RemoveHead ....

}

Sleep(10); // let the OS run another thread

}






Re: Visual C++ Language How to avoid Busy Waiting?

spree

Marius Bancila wrote:

You have to give the other threads a chance to run by calling Sleep().

while(1)

{

if(MyQ.Size()>0)

{

My.Q.RemoveHead ....

}

Sleep(10); // let the OS run another thread

}

Thanks its a good solution :) exactly what I needed





Re: Visual C++ Language How to avoid Busy Waiting?

Sarath.

Bancila,
Sorry I don't think that this is a good solution for the problem. I think usage of multiple threads can resolve issue. my idea is explained below as pseudo code. The other code will be slow in processing I think

HANDLE hEvent = NULL;

BOOL Initialize() // Function does the initialization
{
// Create Event
}

BOOL Add() // Function where add occurs
{
SetEvent(hEvent);
}

DWORD WINAPI Process(LPVOID pVoid)
{
while (1)
{
WaitForSingleObject( hEvent, INFINITE ); // You may also need wait for one more event to exit from this thread nicely. Also you can put some logic with the same event exit from this loop like setting some flags or something and checking for the flag and exit

if(MyQ.Size()>0)

{

My.Q.RemoveHead ....

}

}
}





Re: Visual C++ Language How to avoid Busy Waiting?

Marius Bancila

Using multithreading is a good idea. Sleep suspends the execution of the current thread for at least the specified interval. That is similar to what WaitForSingleObject does: it puts the calling thread in the wait state. What is different is that in your example the loop is executed only when the event was signaled (and WaitForSingleObject returned), and in my case on a regular basis with the period used for Sleep().

Now, I will not try to convice anyone that your approach isn't better, but I don't see problems with mine either.