Francesco Bonizzi
Francesco Bonizzi

Reputation: 5302

QTimer incorrect timing

I have two Qtimers. T1 starts at 100ms. T2 starts at 2ms.

I put some var to control their timing. Why they aren't in time? Sometimes T2 reaches 55, or 54, or 53, but never at the exact condition? I would need that at every T1 timeout, T2 had done 50 timeouts!

Thanks.

Can someone please suggest me other methods of synchronization?

Upvotes: 0

Views: 912

Answers (2)

AlexTheo
AlexTheo

Reputation: 4184

Your timer will never give you the right interval because dependencies on your OS thread, process scheduler. So in order to calculate a valid elapsed time you should get the system time in your timer tick method and calculate the delta between current time and timer start time. For synchronization you need mutexes or semaphores in case of thread synchronization.

Upvotes: 2

escargot agile
escargot agile

Reputation: 22379

You cannot rely on timers for thread synchronization, because CPU timing is never exact. Other applications run in the background of your operating system and tweak the timing, so you cannot rely on it.

You must use other methods of synchronization, such as monitors, semaphores etc.

Upvotes: 3

Related Questions