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