mirror of
https://github.com/micropython/micropython.git
synced 2025-08-31 23:10:42 +02:00
tests/thread: Remove need to sleep to wait for completion in some tests.
Use a lock and a counter instead, and busy wait for all threads to complete. This makes test run faster and they no longer rely on the time module.
This commit is contained in:
@@ -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')
|
||||
|
Reference in New Issue
Block a user