1# test capability for threads to access a shared immutable data structure 2# 3# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd 4 5import _thread 6 7 8def foo(i): 9 pass 10 11 12def thread_entry(n, tup): 13 for i in tup: 14 foo(i) 15 with lock: 16 global n_finished 17 n_finished += 1 18 19 20lock = _thread.allocate_lock() 21n_thread = 2 22n_finished = 0 23 24# the shared data structure 25tup = (1, 2, 3, 4) 26 27# spawn threads 28for i in range(n_thread): 29 _thread.start_new_thread(thread_entry, (100, tup)) 30 31# busy wait for threads to finish 32while n_finished < n_thread: 33 pass 34print(tup) 35