Constant frame rate using threads and win32
Posted on 2004-09-22
HI, my goal is to have a separate rendeirng thread be able to render and a constant frame rate of say, 30fps.
Info: Single CPU, win32 App, openGL, C++.
I have one rendering thread who's sole purpose in life is: to be created, initialized, wait to start, render at a constant frame rate, wait for a signal to stop rendering, then exit. Let's also assume the code is thread safe :)
So, at every 33.3 ms, the thread will execute some rendering functions, then "go to sleep" and do nothing until the next 33.3ms has ellapsed.
Q1: What are good coding techniques in general to accomplish something like this.?
Q2: Please comment on the following 2 methods:
//Sampling Interval, so render to screen
//check the accuracy
//signal from main to exit rendering loop
if (renderingTimer->GetTime()*1000 >= T)
//check the accuracy
//Sampling Interval, so render graphics
The renderingTimer uses the QueryPerformanceCounter, so should be pretty accurate(I think?). In method B, another thread is needed to set threadMain.threadExit to TRUE to stop the loop, where as in Method A, another thread must use the SetEvent() to stop the thread.
In Method A, the sampling time is based on when there is a timeout in the WaitForSingleObject(). How accurate is this?
I'd like to know more about thread priorities and process priority classes. I did a small test to see the effects of thread priorities using the above 2 methods. Console App, main thread, and one single thread for rendering using the default settings.
The main() has the thread render for 5 seconds. And I checked how accurate the sampling times were. Below are the results:
Method A error: 6.0687
Method B error: 0.001158
Method A error: 6.0205
Method B error: 0.00086314
Method A error: 5.9933
Method B error: 0.0010459
Method A error: 5.7194
Method B error: 131200.0168
the error is the sum of the squared errors of each sampling time. The key change is from THREAD_PRIORITY_ABOVE_NORMAL to THREAD_PRIORITY_NORMAL. Can you please comment on the results. Note I did not change the default process priority.
At the end of the day, I need an accurate(as accurate as QueryPerformanceCounter) constant frame rate. What is the solution?
Thanks for your help