Coding Problem (arbitrary thread sych)

Discussion in 'Python' started by peleme, Feb 26, 2006.

  1. peleme

    peleme Guest

    Hi,

    Here is a code example to visualize my problem.

    ----------------------------------------------------------------------------------
    import thread
    import threading
    from time import sleep

    def a():
    print "exec a"
    sleep(1)

    def b():
    print "exec b"
    sleep(4)

    def c():
    print "exec c"
    sleep(3)

    def d():
    print "exec d"
    sleep(2)

    def e():
    print "exec e"
    sleep(2)

    def s():
    print "exec s"
    # ...
    # code which synchronize the incoming three threads
    # ....
    print "in synch"


    def main(sstr):
    nodes = sstr.split(",")
    print nodes
    for n in nodes:
    if n is "a":
    a()
    elif n is "b":
    b()
    elif n is "c":
    c()
    elif n is "d":
    d()
    elif n is "e":
    e()
    elif n is "s":
    s()
    else:
    print "unkown"


    #A
    threading.Thread(target=main, args=("b,c,s,e",)).start()
    #B
    threading.Thread(target=main, args=("c,s,e",)).start()
    #C
    threading.Thread(target=main, args=("s,e",)).start()
    #D
    threading.Thread(target=main, args=("c,e",)).start()
    ----------------------------------------------------------------------------------

    The threads which arrives to the s function should wait for each other
    until the last one. So A, B, and C executes the last function
    'together', while D executes it seperate.
    I only know that three threads have to be in synch.

    I've been now spending a week on the problem.
    The same problem should also be solved in C++. Buts thats another
    issue. (Someone knows, too?)

    Thanks,

    Martin
     
    peleme, Feb 26, 2006
    #1
    1. Advertising

  2. peleme

    Kent Johnson Guest

    peleme wrote:
    > The threads which arrives to the s function should wait for each other
    > until the last one. So A, B, and C executes the last function
    > 'together', while D executes it seperate.
    > I only know that three threads have to be in synch.


    Take a look at the Barrier patter in The Little Book of Semaphores:
    http://greenteapress.com/semaphores/

    It requires that you know in advance how many threads need to rendezvous
    so you will have to preprocess your args a little.

    Kent
     
    Kent Johnson, Feb 26, 2006
    #2
    1. Advertising

  3. peleme

    peleme Guest

    Hi Kent,

    that's exactly the solution for my problem. Thanks a lot!

    Martin
     
    peleme, Feb 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ! aaa
    Replies:
    1
    Views:
    1,077
    ! aaa
    May 28, 2004
  2. Honestmath
    Replies:
    5
    Views:
    569
    Honestmath
    Dec 13, 2004
  3. Replies:
    2
    Views:
    428
    Alf P. Steinbach
    Dec 13, 2004
  4. calmar
    Replies:
    11
    Views:
    917
    calmar
    Feb 21, 2006
  5. whygee
    Replies:
    23
    Views:
    1,592
    Mike Treseler
    Aug 26, 2008
Loading...

Share This Page