8000 add free-threading test · python/cpython@772e7e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 772e7e0

Browse files

committed
add free-threading test
1 parent 152fed3 commit 772e7e0

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

Lib/test/test_free_threading/test_dict.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from ast import Or
77
from functools import partial
8-
from threading import Thread
8+
from threading import Barrier, Thread
99
from unittest import TestCase
1010

1111
try:
@@ -142,6 +142,27 @@ def writer_func(l):
142142
for ref in thread_list:
143143
self.assertIsNone(ref())
144144

145+
def test_racing_get_set_dict(self):
146+
"""Races getting and setting a dict should be thread safe"""
147+
THREAD_COUNT = 10
148+
barrier = Barrier(THREAD_COUNT)
149+
def work(d):
150+
barrier.wait()
151+
for _ in range(1000):
152+
d[10] = 0
153+
d.get(10, None)
154+
_ = d[10]
155+
156+
d = {}
157+
worker_threads = []
158+
for ii in range(THREAD_COUNT):
159+
worker_threads.append(Thread(target=work, args=[d]))
160+
for t in worker_threads:
161+
t.start()
162+
for t in worker_threads:
163+
t.join()
164+
165+
145166
def test_racing_set_object_dict(self):
146167
"""Races assigning to __dict__ should be thread safe"""
147168
class C: pass

0 commit comments

Comments
 (0)
0