PDA

View Full Version : QThread msleep precision



Lele
8th November 2007, 20:27
Hi,
I have an application where I need to have precise (at millisecond) timing.
I know I cannot trust QTimer, since the app is quite big and I think using Qtimer I accumulate delays.
Let's say, I run a startTimer(5), if I check using QTime::currentTime(), I notice a delay around 10-15 ms instead of 5.
So I tried adding a simple QThread to the app, just to see if in another thread I can be more precise.



void testThread::run()
{
while (!m_stop)
{
m_currTime = QTime::currentTime();
int elap = m_oldTime.msecsTo(m_currTime);
m_oldTime = m_currTime;
QString tt = QString("delay:%1").arg(elap);
qDebug() << tt;
msleep(10);
}
}


And even like this the delay is 15-16, sometimes 0 ! and if the app has some blocking operation even the thread is affected.

The Cpu is always at 50%, so am I doing something wrong?
Or that's normal? I would expect to have the thread runnig separatedly from the main process.

thanks for any idea
bye

wysota
8th November 2007, 20:41
To me it looks like you have a two processor board and all threads of the process have the same CPU affinity resulting in one CPU being idle all the time. Anyway if you need precise timing, you need to use a real-time operating system (or at least its emulation like real-time priority scheduling in regular Linux). Otherwise you'll be lagging behind. Always.

Your code looks fine, by the way. Maybe you are starting the thread in a wrong way? Did you call QThread::start() to start the thread?

Lele
8th November 2007, 21:52
thanks for replying,
of course I use Qthread::start.

The thing if the thread is still affected by what's happening in the main process it seems useless, at least in my case.

So I was thinking maybe should I test other thread like SDL....

I'm building a video player and I need to check frequently the AV sync, every delay results in lag while playing the video.

bye

wysota
8th November 2007, 23:40
You can check the sync while displaying a frame, you don't need threads for that. Keep in mind that threads slow down your application, contrary to a common belief of being otherwise.