Question About Threads

Started by
3 comments, last by DRPhil 18 years, 7 months ago
I created windows app using VC++. The app containes multiple threads that I created. One thread reads a text file into memory for display onto the screen. The thread reads a certain number of lines before flagging a another thread to draw it to the screen. It works fine when I do not have a sleep in my reader thread. However, the data updates to the screen way too fast. I place a small sleep (a few milliseconds) and the thing slows down to a snail's pace. However, the strange thing is that is sometimes runs fine (ie draws to screen; rest a short time; draw again). It seems pretty random. I close it down and start it again without changing anything. Sometimes it runs fine (one second between updates); sometimes it updates very slowly (a few seconds even though my sleep is a few milliseconds). To be honest, the one second between updates seems a little slow. I created all of my threads with the same priority, THREAD_PRIORITY_NORMAL. I've never come across a problem like this before. Any ideas? Thanks.
Advertisement
How does your synchronization mechanism between the two threads work?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I don't think it's anything with the sync. I found something strange. This is the prime loop inside my thread.

while(bFinished == false)
{
if(reset == false)
{
reset = true;
begin = GetTickCount();
}

Sleep(1); //hard code it just incase I'm setting
//member wrong
//check event status
EventState = WaitForSingleObject(m_hStopEvent, 0);

switch(EventState)
{
case WAIT_TIMEOUT:

//do something..............

if(reset == true)
{
finish = GetTickCount ();
duration = finish - begin;

TRACE(_T("Timer: %d\n"), duration);
reset = false;
}



break;
case WAIT_OBJECT_0:
//end event has been signaled
bFinished = true;
break;
default:
status = -1;
break;
}
}


I ran my app and the out put was strange. Sometimes, it would give a reading of zero millisecond duration. Other times, it would say 16 millisecond. I've commented out the section which actually does anything. Even when only the wait for event and sleep are present, the app still displays this strange behavior. It's the oddest thing I've seen.
Sounds like a timer resolution issue to me... down in that short of a timespan it's unlikely that the kernel scheduler will give you back control in precisely the number of milliseconds you ask for, especially if your thread priority is set to Normal.

What are you aiming for your application to do? How are you intending for it to look? There's probably a simpler or at least more effective way to accomplish what you're after.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I'm unsure if the forum automatically left-aligns the text, or that is the way you have decided to use spacing. If it isn't your spacing, please put it in the "[s0urce]...[/s0urce]" blocks, because it is difficult to read code like that.

Additionally, saying if(boolean == true) is redundant. While it does work, if someone wrote if(true == true), it would be silly because true always equals true. All you need to say is if(boolean) or if(!boolean) and not compare it to true or false.

This topic is closed to new replies.

Advertisement