PDA

View Full Version : Problem about timer in multithread programs



vql
16th October 2007, 13:43
Because the forum is not support the space charactor -> so i replace it with *

****obj*******obj***
A ----------> B ----------> C
****Bus*******Car***

In this model, Bus & Car is thread objects. Bus will move obj from A to B in 10 seconds. After that, Car will move obj from B to C in 10 seconds. The contraint is that Bus must finish before Car starts.

To make a look-feel as Bus is running, I divide AB to 10 small distances, & I set interval for steps is 1 second because 10 steps * 1 second = 10 seconds -> this is the time duration for Bus finish AB. But when I use this way, in fact Bus will finish more 10 seconds, because we must plus the time for calculations in one step. So, we have:
The time duration to finish AB = (1 + time_for_one_step)*10

Although time_for_one_step is very small but If I divide AB smaller, the extra time is bigger.
My problem is that how to Bus allways finish AB before 10 seconds but not too early.

Thanks. Please help me.

marcel
16th October 2007, 13:48
Why don't you synchronize the threads, Bus and Car? Don't let Car start until Bus has finished? You can either use wait conditions or a global volatile flag or you could connect Bus's finished() signal to Car's start() slot - but this only if you are using Qt.

vql
16th October 2007, 14:37
Why don't you synchronize the threads, Bus and Car? Don't let Car start until Bus has finished? You can either use wait conditions or a global volatile flag or you could connect Bus's finished() signal to Car's start() slot - but this only if you are using Qt.

My purpose is that how to run Bus in 10 seconds. Because my program is 3D simulation, it requires all objects must finish on time. If Bus doesn't finish in 10 seconds, Car will have to wait the Bus until it finish -> Car will be late. And if the model have some threads (as Truck move obj from C to D, ....), it will make the back threads run later.

Really, with your solution, Bus will not move to B after 10 seconds -> Car will not move to C after 20 seconds (plus 10 seconds of Car).

marcel
16th October 2007, 16:52
You can't do it in exactly 10 seconds. Apart from your considerations you also have to take into account the process scheduler, context switches and the time other processes are running on your "10" seconds.

This is pretty difficult to achieve and you couldn't do it even on multiple core processors.
Maybe on a real-time OS you could find the support to do it.

But, with the common, desktop OS's , something AROUND 10 seconds should be enough.
So, if you are confident that additional computations don't take more than 1 sec at each step, then in an asymptotic analysis they could be ignored.

Otherwise you can do an analysis of you algorithm and derive the average time it takes to run.

vql
17th October 2007, 15:00
You can't do it in exactly 10 seconds. Apart from your considerations you also have to take into account the process scheduler, context switches and the time other processes are running on your "10" seconds.

This is pretty difficult to achieve and you couldn't do it even on multiple core processors.
Maybe on a real-time OS you could find the support to do it.

But, with the common, desktop OS's , something AROUND 10 seconds should be enough.
So, if you are confident that additional computations don't take more than 1 sec at each step, then in an asymptotic analysis they could be ignored.

Otherwise you can do an analysis of you algorithm and derive the average time it takes to run.

Thank you. How can I finish it in 10 seconds but not too early? Please give me your solution.