Sign in to follow this  

Windows Threads

This topic is 4842 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 am writing a win32 application using C++ and win api functions via windows.h I am trying to implement threads so that they do not use the CPU while they are not working. Here is an example of how I implement a thread currently. In this case it's task is to draw.
GLOBAL BOOL dying;

DWORD ThreadProceedure()
{
  while(!dying)
  {
    if (TIME_TO_DRAW)
      DRAW_FRAME;
  }
  return 0;
}

This while loop is extremely nasty as the CPU is being used constantly, even if I were to set it to only draw one frame per minute. It would be nicer if: -The thread puts itself to sleep once all tasks are performed -The thread can be awakened by external messages sent from other threads -The thread can set up some sort of timer system that will post a message to itself at a given interval in the manner of WM_TIMER What I really need is something like the get/translate/dispatch loop you find in a lot of win32 apps:
bool hret;
while ((hret = GetMessage (&messages, NULL, 0, 0)) != 0)
{
   TranslateMessage(&messages);
   DispatchMessage(&messages);
}

GetMessage does not return until there is a message in the threads message queue. Therefore whilst there is no activity, there is also no CPU usage. As it should be. Unfortunately to use GetMessage for my thread I would have to set up a dummy window. Good idea? Or is there some sort of messaging system I don't know about in the windows API that would be handy?

Share this post


Link to post
Share on other sites
Every thread in a Windows process has its own messaging queue. You can use SendMessage and GetMessage to send and retrieve messages from the thread queue (read the documentation carefully; there are subtle nuances). Alternately, if you don't need all that - say all you need is just synchronization with another thread - then look at the wait functions WaitForSingleObject, WaitForMultipleObject, etc, and the Sleep function.

Share this post


Link to post
Share on other sites
Wouldn't you want your drawing thread to be constantly running?

Or are you talking about other threads and used the drawing one only as an example?

In that case, give examples of those other ones. Different threads block in different ways.

Share this post


Link to post
Share on other sites
Use Sleep(1) (SleepEx(1,bool) in this case) if you want to allow lower priority threads to run. As stated in the docs, Sleep(0) only allows threads of equal or greater priority to run afterwards.

Share this post


Link to post
Share on other sites
I'd still suggest SleepEx(0,TRUE) for threads that do repetitive fine-grained tasks.

If you spin the thread for a full ms, another thread may claim the cpu a longer time, so your spinning thread misses some time-slices.

But I would try both, since changing the single time-parameter is not a big modification.

Share this post


Link to post
Share on other sites

This topic is 4842 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this