Skip to content

Commit 2847d74

Browse files
committed
tests/thread: Replace busy waiting loops with a loop that sleeps.
Depending on the thread scheduler, a busy-wait loop can hog the CPU and make the tests very slow. So convert such loops to loops that have an explicit sleep, allowing the worker threads to do their job.
1 parent f2d732f commit 2847d74

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

tests/thread/stress_heap.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#
44
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
55

6+
try:
7+
import utime as time
8+
except ImportError:
9+
import time
610
import _thread
711

812
def last(l):
@@ -37,6 +41,6 @@ def thread_entry(n):
3741
for i in range(n_thread):
3842
_thread.start_new_thread(thread_entry, (10000,))
3943

40-
# busy wait for threads to finish
44+
# wait for threads to finish
4145
while n_finished < n_thread:
42-
pass
46+
time.sleep(1)

tests/thread/thread_lock4.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

5+
try:
6+
import utime as time
7+
except ImportError:
8+
import time
59
import _thread
610

711
def fac(n):
@@ -39,6 +43,7 @@ def thread_entry():
3943
with jobs_lock:
4044
if len(output) == n_jobs:
4145
break
46+
time.sleep(1)
4247

4348
# sort and print the results
4449
output.sort(key=lambda x: x[0])

tests/thread/thread_qstr1.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

5+
try:
6+
import utime as time
7+
except ImportError:
8+
import time
59
import _thread
610

711
# function to check the interned string
@@ -28,8 +32,8 @@ def th(base, n):
2832
for i in range(n_thread):
2933
_thread.start_new_thread(th, (i * n_qstr_per_thread, n_qstr_per_thread))
3034

31-
# busy wait for threads to finish
35+
# wait for threads to finish
3236
while n_finished < n_thread:
33-
pass
37+
time.sleep(1)
3438

3539
print('pass')

0 commit comments

Comments
 (0)