Problem about timer in multithread programs

Discussion in 'C' started by vql, Oct 13, 2007.

  1. vql

    vql New Member

    Joined:
    Sep 27, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Don't let thread B start until A is finished.
     
  3. vql

    vql New Member

    Joined:
    Sep 27, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. vql

    vql New Member

    Joined:
    Sep 27, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  6. vql

    vql New Member

    Joined:
    Sep 27, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    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).
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice