Threading..

Started by
1 comment, last by Cornutopia 19 years, 6 months ago
I've been adding threading to my application the last few days as I want to run the physics engine in a separate thread which runs much faster than the rendering thread. I cannot get my physics thread working anymore than 62-65fps even though the main thread is running over 300fps and has a lower priority than the worker thread. Even if the main thread is pushing a lot of polys and the fps drops to say 88.. the phsyics thread still runs around 65fps.. Does anybody have experience in this in win32 ?? I'm creating the thread as per normal

m_fPhysicsThreadTerminate = FALSE;
m_hPhysicsThread = (HANDLE)_beginthreadex(NULL,
                 0,
                 myPhysicsThread,
                 (void *)this,
                 CREATE_SUSPENDED,
                 (unsigned int *)&m_PhysicsThreadID);

SetThreadPriority(m_hPhysicsThread, THREAD_PRIORITY_HIGHEST);
ResumeThread(m_hPhysicsThread);

and the actual thread code..

#define PHYSICS_UPDATE_FPS      300
#define PHYSICS_THREAD_SLEEPTIME   (1000 / PHYSICS_UPDATE_FPS)

unsigned int __stdcall myPhysicsThread(void *parm)
{
   __int64   timer;
   DWORD   LastTickCount;               // Tick Counter
   float   TimerResolution;            // Timer Resolution
   DWORD   StartTime, EndTime;            // Ticks for Physics Usage

   // set reference to game player class
   GamePlayer *pgp = (GamePlayer *)parm;

   // must support high res timers

   // Grab The Starting Tick Value
   QueryPerformanceCounter((LARGE_INTEGER *)&timer);   // Grab Current Value In Performance Counter
   LastTickCount = (DWORD)timer;

   // Grab The Counter Frequency
   QueryPerformanceFrequency((LARGE_INTEGER *) &timer);
   // Set The Timer Resolution 1.0f / Timer Frequency
   TimerResolution = (float) (((double)1.0f)/((double)timer));

   // while the parent thread dosn't want us to quit..
   while(pgp->m_fPhysicsThreadTerminate == FALSE)
   {
      // get the current tick..
      QueryPerformanceCounter((LARGE_INTEGER *)&timer);
      StartTime = (DWORD)timer;

      // get the number of milliseconds since the last thread slice..
      float milliseconds = (float)(StartTime - LastTickCount) * TimerResolution * 1000.0f;

//      NewtonUpdate(pgp->m_pNewtonWorld, (milliseconds / 1000));      // convert to seconds

      LastTickCount = StartTime;

      pgp->m_PhysicsTickCount++;

      Sleep(PHYSICS_THREAD_SLEEPTIME);
   }

   _endthread();

   return (TRUE);
}

At the moment I've commented out the newtonupdate though it works well there just to see if I can get the thread running at something even close to the required FPS. I've tried playing with all the prioritys and even littering the rendering pipeline with Sleep(0)'s which should cause a task switch (if a task is available) I'm using Mutex's to safeguard Newton and the Rendering Engine, though I've cut them out while working out while I'm getting this problem.. anybody got any ideas.. ?? Cheers Chris
Advertisement
If memory serves, in Win32 the thread priority really doesn't do much alot of the time. And when you create a thread with a "different" priority (in this case, highest) it is not really of that priority, but rather given that priority relative to the thread that created it. But maybe I just forgot what that part of the book said.

That being said, threads are marvelous things but unless you use lots of mutex-style things you really can't assume what's going to run at any one time. If you want to do your own scheduling I think Win32 does that with "fibers."

Also, if you're telling the physics thread to sleep and the main thread not to, the main thread will run alot more. 3ms (which is what you're passing to Sleep) is a VERY long time inside the computer.

But it's late now, so I could be completely wrong.
The best way to guarantee a speed differential is to ignore threading and do it yourself in one thread. Also, note that different OS's react differently to priority. A program I wrote that was adequately fast on Win98 locked up XP completely in an infinite loop of obeyance!

Mark
Ever wanted to command a starship?
http://www.lostinflatspace.com

This topic is closed to new replies.

Advertisement