1# test threads sleeping 2# 3# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd 4 5try: 6 import utime 7 8 sleep_ms = utime.sleep_ms 9except ImportError: 10 import time 11 12 sleep_ms = lambda t: time.sleep(t / 1000) 13 14import _thread 15 16lock = _thread.allocate_lock() 17n_thread = 4 18n_finished = 0 19 20 21def thread_entry(t): 22 global n_finished 23 sleep_ms(t) 24 sleep_ms(2 * t) 25 with lock: 26 n_finished += 1 27 28 29for i in range(n_thread): 30 _thread.start_new_thread(thread_entry, (10 * i,)) 31 32# wait for threads to finish 33while n_finished < n_thread: 34 sleep_ms(100) 35print("done", n_thread) 36