Problem about timer in multithread programs

vql's Avatar, Join Date: Sep 2007
vql
Go4Expert Member
I am writting a multithread program using Qt (or any libraries). I will demonstrate about it:

All threads will be controled by the message mechanism. When a message send from a external program, my program will catch it & read information from the message and do the correlative task. Simplely, the external program only send 2 messages, the distance to send between 2 messages is 5 seconds. Each message need a correlative thread because in the received message have information about object will do this task & thread objects are different classes.

The first message: a threadA need 5 seconds to move objectA from X to Y. In duration, threadA will lock objectA to restrict other threads move it. After 5 seconds (after threadA started), a message received, a threadB will need 5 seconds to continuely move objectA from Y to Z.

If I set timer for threadA is 5 seconds but in fact, the system will need 5 seconds & 100 miliseconds ( 100 miliseconds only as a example, and it depend on CPU's speed) because when threadA started, it lost some miliseconds to do its tasks.

Like this, when threadB started, it can't control objectA because in this time, objectA is still controled by threadA.

I see that the multithread program depends on CPU's speed. How to solve this program so that threadA allways finish before threadB start.

Thanks.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Don't let thread B start until A is finished.
vql's Avatar, Join Date: Sep 2007
vql
Go4Expert Member
That is mean: I need to manage all threads in the program. When a thread created, I will save it to a list to manage, because only with this way, threadB can know threadA is running or finished. It is right? Please give me some suggest! Thanks.
vql's Avatar, Join Date: Sep 2007
vql
Go4Expert Member
Maybe, I give you another example. Please see this structure:

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.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You're misunderstanding me. Google semaphores, locks, mutexes.

Incidentally, if you want the post to support space characters, use code tags. See the "Before you make a query" thread.
vql's Avatar, Join Date: Sep 2007
vql
Go4Expert Member
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).
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You may not realize it (you should), but tens of thousands of people, if not millions, have solved this problem. Read your responses and follow up on them as suggested.