diff --git a/tests/thread/thread_exc1.py b/tests/thread/thread_exc1.py index 9ef3d2a154..10fb94b4fb 100644 --- a/tests/thread/thread_exc1.py +++ b/tests/thread/thread_exc1.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(): @@ -16,9 +12,19 @@ def thread_entry(): foo() except ValueError: pass + with lock: + global n_finished + n_finished += 1 -for i in range(4): +lock = _thread.allocate_lock() +n_thread = 4 +n_finished = 0 + +# spawn threads +for i in range(n_thread): _thread.start_new_thread(thread_entry, ()) -time.sleep(0.2) +# busy wait for threads to finish +while n_finished < n_thread: + pass print('done') diff --git a/tests/thread/thread_ident1.py b/tests/thread/thread_ident1.py index f329566764..217fce73b1 100644 --- a/tests/thread/thread_ident1.py +++ b/tests/thread/thread_ident1.py @@ -2,20 +2,20 @@ # # 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 thread_entry(): tid = _thread.get_ident() print('thread', type(tid) == int, tid != 0, tid != tid_main) + global finished + finished = True tid_main = _thread.get_ident() print('main', type(tid_main) == int, tid_main != 0) +finished = False _thread.start_new_thread(thread_entry, ()) -time.sleep(0.2) +while not finished: + pass print('done') diff --git a/tests/thread/thread_shared1.py b/tests/thread/thread_shared1.py index 6ef4b654f8..13c6651cc4 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) diff --git a/tests/thread/thread_shared2.py b/tests/thread/thread_shared2.py index 3e3e2dc33a..e4bfe78022 100644 --- a/tests/thread/thread_shared2.py +++ b/tests/thread/thread_shared2.py @@ -3,10 +3,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(lst, i): @@ -15,11 +11,22 @@ def foo(lst, i): def thread_entry(n, lst, idx): for i in range(n): foo(lst, idx) + with lock: + global n_finished + n_finished += 1 +lock = _thread.allocate_lock() +n_thread = 2 +n_finished = 0 + +# the shared data structure lst = [0, 0] -_thread.start_new_thread(thread_entry, (10, lst, 0)) -_thread.start_new_thread(thread_entry, (20, lst, 1)) -# wait for threads to finish -time.sleep(0.2) +# spawn threads +for i in range(n_thread): + _thread.start_new_thread(thread_entry, ((i + 1) * 10, lst, i)) + +# busy wait for threads to finish +while n_finished < n_thread: + pass print(lst) diff --git a/tests/thread/thread_stacksize1.py b/tests/thread/thread_stacksize1.py index b0118843b6..e3e77fadaf 100644 --- a/tests/thread/thread_stacksize1.py +++ b/tests/thread/thread_stacksize1.py @@ -3,10 +3,6 @@ # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd import sys -try: - import utime as time -except ImportError: - import time import _thread # different implementations have different minimum sizes @@ -20,6 +16,9 @@ def foo(): def thread_entry(): foo() + with lock: + global n_finished + n_finished += 1 # test set/get of stack size print(_thread.stack_size()) @@ -27,10 +26,16 @@ print(_thread.stack_size(sz)) print(_thread.stack_size() == sz) print(_thread.stack_size()) +lock = _thread.allocate_lock() +n_thread = 2 +n_finished = 0 + # set stack size and spawn a few threads _thread.stack_size(sz) -for i in range(2): +for i in range(n_thread): _thread.start_new_thread(thread_entry, ()) -time.sleep(0.2) +# busy wait for threads to finish +while n_finished < n_thread: + pass print('done') diff --git a/tests/thread/thread_stress_recurse.py b/tests/thread/thread_stress_recurse.py index eb9c136f25..68367c4dd7 100644 --- a/tests/thread/thread_stress_recurse.py +++ b/tests/thread/thread_stress_recurse.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(): @@ -16,8 +12,14 @@ def thread_entry(): foo() except RuntimeError: print('RuntimeError') + global finished + finished = True + +finished = False _thread.start_new_thread(thread_entry, ()) -time.sleep(0.2) +# busy wait for thread to finish +while not finished: + pass print('done')