Coding Problem (arbitrary thread sych)

P

peleme

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
 
K

Kent Johnson

peleme said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top