Skip to content

Commit 2e4cdae

Browse files
committed
tests/thread: Add test for concurrent interning of strings.
Qstr code accesses global state and needs to be made thread safe.
1 parent 094a0dd commit 2e4cdae

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/thread/thread_qstr1.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# test concurrent interning of strings
2+
#
3+
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
4+
5+
import _thread
6+
7+
# function to check the interned string
8+
def check(s, val):
9+
assert type(s) == str
10+
assert int(s) == val
11+
12+
# main thread function
13+
def th(base, n):
14+
for i in range(n):
15+
# this will intern the string and check it
16+
exec("check('%u', %u)" % (base + i, base + i))
17+
18+
with lock:
19+
global n_finished
20+
n_finished += 1
21+
22+
lock = _thread.allocate_lock()
23+
n_thread = 4
24+
n_finished = 0
25+
26+
# spawn threads
27+
for i in range(n_thread):
28+
_thread.start_new_thread(th, (i * 1000, 1000))
29+
30+
# busy wait for threads to finish
31+
while n_finished < n_thread:
32+
pass
33+
34+
print('pass')

0 commit comments

Comments
 (0)