Coding Problem (arbitrary thread sych)

peleme i5ex1rbkxp19gxl at jetable.net
Sun Feb 26 12:42:24 EST 2006


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




More information about the Python-list mailing list