Optimal task switching, how do You do it?

Started by
2 comments, last by random 23 years, 10 months ago
Hi all, [edit: egads that blue looks horrid...] I've got my basic libraries working and I am questioning the way I am doing things in WinMain() and the various callback functions. I really hate it when I do this because I usually (this would be the 4th time) end up rewriting huge sections of my libraries. Anyways, my question is what is the "best" way to do the task switching between processes? Should I switch everything off in the while loop in WinMain(), or make a thread that switches all the processes off, or make everything a thread and lock all the threads when I go to draw? (GAK!) The reason that I care is that right now I am getting 100fps (limited by my refresh rate) but if I have 100,000 objects in the game, I get a Huge pause (5 to 6 seconds) whenever I go to update them. I don't envision actually *having* 100,000 objects in the game at one time, but the fact that it altogether stops rather irritates me. Right now, my main.cpp file looks kind of like this. The red sections are where I am switching out my tasks.

void FPS30_ThreadProc(void *param)
{
  int lastTime = clock();
  while (1)
  {
    int currTime = clock();
    int sleepTime = 0;


    // run these processes once every 30ms (or try...)
    interface.process();
    objectmanager.process();


    sleepTime = 33 - (currTime - lastTime);
    if (sleepTime > 0)
      Sleep(sleepTime);

    lastTime = currTime;
  }
  _endthread();
}

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmd, int show)
{
  // windows stuff... register, create windows, etc

  initGame();

  while(1)
  {
    // handle messages

    // do these as fast as possible
    if (checkScroll()) doScroll();
    map.blit(mapx, mapy);
    screen.publish();

  }
  return 1;
}
  
That's all nice and good, but another way I thought of doing it was to make this change:

void FPS30_ThreadProc(void *param)
{
  int lastTime = clock();
  while (1)
  {
    int currTime = clock();


    // do these as fast as possible
    if (checkScroll()) doScroll();
    map.blit(mapx, mapy);
    screen.publish();

    if (currTime - lastTime > 32)
    {


      // run these processes once every 30ms (or try...)
      interface.process();
      objectmanager.process();


      lastTime = currTime;
    }

  }
  _endthread();
}
  
There is also the third option, to have everything in the WinMain()'s while loop and have them run procedurally. Then, there's the multithreaded approach to doing it. Every time I go to update the interface (check for clicks, etc) have it spawn a thread per object. Every game object (tanks, airplanes, buildings, etc) could also spawn a process to decide what to do and another one to actually do it. So, here I am asking the question of "How do You do it?" Indecision is such a wonderous thing... Thanks all! random --- Wait, you mean I have to actually... THINK?! Edited by - random on 5/30/00 4:51:04 PM
---Wait, you mean I have to actually... THINK?!
Advertisement
Well, spawning a thread per objects sounds like abuse of multithreading to me That might slow down your stuff anyway, because remember that one processor can still only take one instruction at a time...
actually spawning new processes to do multi-tasking is really not a good thing if it is not necessary. if you knew how many instructions it takes to switch task gates and how many cycles that takes compared to an "if" and "call" statement. i think you would really not want to implement the problem that way.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Multiple threads CAN slow your computer down bigtime!

If you don''t have to - don''t use it!

If you have to - check out "WaitForSingleObject" Win32 function! This will make the thread wait, and consume no CPU time (almost). "MsgWaitForMultipleObjects" is even more interresting...

This topic is closed to new replies.

Advertisement