diff options
Diffstat (limited to 'tests/thread/thread_shared1.py')
-rw-r--r-- | tests/thread/thread_shared1.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/tests/thread/thread_shared1.py b/tests/thread/thread_shared1.py index 6ef4b654f..13c6651cc 100644 --- a/tests/thread/thread_shared1.py +++ b/tests/thread/thread_shared1.py @@ -2,10 +2,6 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd -try: - import utime as time -except ImportError: - import time import _thread def foo(i): @@ -14,11 +10,22 @@ def foo(i): def thread_entry(n, tup): for i in tup: foo(i) + with lock: + global n_finished + n_finished += 1 +lock = _thread.allocate_lock() +n_thread = 2 +n_finished = 0 + +# the shared data structure tup = (1, 2, 3, 4) -_thread.start_new_thread(thread_entry, (100, tup)) -_thread.start_new_thread(thread_entry, (100, tup)) -# wait for threads to finish -time.sleep(0.2) +# spawn threads +for i in range(n_thread): + _thread.start_new_thread(thread_entry, (100, tup)) + +# busy wait for threads to finish +while n_finished < n_thread: + pass print(tup) |