Velocity Reviews > Coding Problem (arbitrary thread sych)

# Coding Problem (arbitrary thread sych)

peleme
Guest
Posts: n/a

 02-26-2006
Hi,

Here is a code example to visualize my problem.

----------------------------------------------------------------------------------
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
#B
#C
#D
----------------------------------------------------------------------------------

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

Kent Johnson
Guest
Posts: n/a

 02-26-2006
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

peleme
Guest
Posts: n/a

 02-26-2006
Hi Kent,

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

Martin