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

## Recommended Posts

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;
0,
(void *)this,
CREATE_SUSPENDED,


#define PHYSICS_UPDATE_FPS      300

{
__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..
{
// 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++;

}

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

##### Share on other sites
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.

##### Share on other sites
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

• 17
• 10
• 21
• 16
• 9