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