Skip to content

Commit 32d767f

Browse files
committed
use run_concurrent to spawn threads
1 parent 27d6cb6 commit 32d767f

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

Lib/test/test_free_threading/test_heapq.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import heapq
44

55
from enum import Enum
6-
from threading import Thread, Barrier
6+
from threading import Thread, Barrier, Lock
77
from random import shuffle, randint
88

9-
from test.support import threading_helper, Py_GIL_DISABLED
9+
from test.support import threading_helper
1010
from test import test_heapq
1111

1212

@@ -178,39 +178,32 @@ def heapreplace_max_func(max_heap, replace_items):
178178
self.assertEqual(len(max_heap), OBJECT_COUNT)
179179
self.test_heapq.check_max_invariant(max_heap)
180180

181-
@unittest.skipUnless(Py_GIL_DISABLED, 'only used to test under free-threaded build')
182181
def test_lock_free_list_read(self):
183182
n, n_threads = 1_000, 10
184183
l = []
185184
barrier = Barrier(n_threads * 2)
186185

187-
def writer():
188-
barrier.wait()
189-
for i in range(n):
190-
heapq.heappush(l, 1)
191-
heapq.heappop(l)
186+
count = 0
187+
lock = Lock()
188+
189+
def worker():
190+
with lock:
191+
nonlocal count
192+
x = count
193+
count += 1
192194

193-
def reader():
194195
barrier.wait()
195196
for i in range(n):
196-
try:
197-
l[0]
198-
except IndexError:
199-
pass
200-
201-
import threading
202-
threads = []
203-
with threading_helper.catch_threading_exception() as cm:
204-
for _ in range(n_threads):
205-
t1 = threading.Thread(target=writer)
206-
t2 = threading.Thread(target=reader)
207-
threads.append(t1)
208-
threads.append(t2)
209-
t1.start()
210-
t2.start()
211-
212-
for t in threads:
213-
t.join()
197+
if x % 2:
198+
heapq.heappush(l, 1)
199+
heapq.heappop(l)
200+
else:
201+
try:
202+
l[0]
203+
except IndexError:
204+
pass
205+
206+
self.run_concurrently(worker, (), n_threads * 2)
214207

215208
@staticmethod
216209
def is_sorted_ascending(lst):

0 commit comments

Comments
 (0)