1# test thread coordination using a lock object 2# 3# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd 4 5import _thread 6 7lock = _thread.allocate_lock() 8n_thread = 10 9n_finished = 0 10 11 12def thread_entry(idx): 13 global n_finished 14 while True: 15 with lock: 16 if n_finished == idx: 17 break 18 print("my turn:", idx) 19 with lock: 20 n_finished += 1 21 22 23# spawn threads 24for i in range(n_thread): 25 _thread.start_new_thread(thread_entry, (i,)) 26 27# busy wait for threads to finish 28while n_finished < n_thread: 29 pass 30