View Single Post
  #2  
Old 02-19-2008, 02:10 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
The problem is that your script does not wait for the threads to complete before exiting. I've modified your script to use the more advanced threading module. It will also wait for all the threads to complete before exiting. Here is the code:
Code:
import threading

#Create mutex for printing
printLock = threading.Lock()

def countingThread(id, maxVal):
	global printLock
	for i in range(maxVal):
		printLock.acquire()
		print "Thread ", id, ": ", i
		printLock.release()

threads = []
for i in range(5):
	#Create and start the threads
	t = threading.Thread(target=countingThread, args=(i, 50))
	t.start()
	
	#Save thread object to list
	threads.append(t)

#Wait for threads to complete
for t in threads:
	t.join()
Reply With Quote