Click to See Complete Forum and Search --> : SetEvent taking too long


TheSauce
July 28th, 2005, 07:08 PM
Hi, I'm using SetEvent and WaitForSingleObject to stop a worker thread when its run out of work to do and to wake it back up when the main thread has given it more work to do.
The event is created using CreateEvent(NULL, false, false, NULL);
The event handle is a volitile varible in a class that both threads have a pointer to.

The main thread calls SetEvent each time it adds more data for the worker thread. The worker thread tends to go to sleep quite often (15 times a second or so).

My problem is that the call to SetEvent takes 4milliseconds 75%+ of the time. This is on a AMD64 3200+. This is clearly a huge amount of time. It's quick sometimes, but it's usually slow

So the question is what am I doing wrong? I assume this isn't the normal behavior of this function, so does anyone have any suggestions on what could be causing this?

Thanks a lot

Malcolm

Kanker Noob
July 29th, 2005, 12:01 AM
how are you handling the looping i asume its in a loop because you sayd wait till it has more work todo

TheSauce
July 29th, 2005, 12:08 PM
The code the worker thread executes is essentially

while (1)
{
if (!doSomeWork())
{
WaitForSingleObject(myEvent, INFINITE);
}
}


doSomeWork returns false if there was no work to do.

golanshahar
July 29th, 2005, 12:35 PM
what priority does the thread has? if its normal try to use ::SetThreadPriority(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setthreadpriority.asp) to give the thread higher priority in order to see if the amount of time is decreasing.

and another question, how many threads are you running there?

Cheers