Jump to content
  • Advertisement
Sign in to follow this  
fathom88

Question About Threads

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!