@@ -165,100 +165,98 @@ def test_join_thread(self):
165165
166166 def task ():
167167 time .sleep (0.05 )
168- finished .append (None )
168+ finished .append (thread . get_ident () )
169169
170170 with threading_helper .wait_threads_exit ():
171- joinable = True
172- ident = thread .start_new_thread (task , (), {}, joinable )
173- thread .join_thread (ident )
171+ handle = thread .start_joinable_thread (task )
172+ handle .join ()
174173 self .assertEqual (len (finished ), 1 )
174+ self .assertEqual (handle .ident , finished [0 ])
175175
176176 def test_join_thread_already_exited (self ):
177177 def task ():
178178 pass
179179
180180 with threading_helper .wait_threads_exit ():
181- joinable = True
182- ident = thread .start_new_thread (task , (), {}, joinable )
181+ handle = thread .start_joinable_thread (task )
183182 time .sleep (0.05 )
184- thread . join_thread ( ident )
183+ handle . join ( )
185184
186- def test_join_non_joinable (self ):
185+ def test_join_several_times (self ):
187186 def task ():
188187 pass
189188
190189 with threading_helper .wait_threads_exit ():
191- ident = thread .start_new_thread (task , ())
190+ handle = thread .start_joinable_thread (task )
191+ handle .join ()
192192 with self .assertRaisesRegex (ValueError , "not joinable" ):
193- thread .join_thread (ident )
193+ handle .join ()
194+
195+ def test_joinable_not_joined (self ):
196+ handle_destroyed = thread .allocate_lock ()
197+ handle_destroyed .acquire ()
194198
195- def test_join_several_times (self ):
196199 def task ():
197- pass
200+ handle_destroyed . acquire ()
198201
199202 with threading_helper .wait_threads_exit ():
200- joinable = True
201- ident = thread .start_new_thread (task , (), {}, joinable )
202- thread .join_thread (ident )
203- with self .assertRaisesRegex (ValueError , "not joinable" ):
204- thread .join_thread (ident )
203+ handle = thread .start_joinable_thread (task )
204+ del handle
205+ handle_destroyed .release ()
205206
206207 def test_join_from_self (self ):
207208 errors = []
208- start_new_thread_returned = thread .allocate_lock ()
209- start_new_thread_returned .acquire ()
209+ handles = []
210+ start_joinable_thread_returned = thread .allocate_lock ()
211+ start_joinable_thread_returned .acquire ()
210212 task_tried_to_join = thread .allocate_lock ()
211213 task_tried_to_join .acquire ()
212214
213215 def task ():
214- ident = thread .get_ident ()
215- # Wait for start_new_thread() to return so that the joinable threads
216- # are populated with the ident, otherwise ValueError would be raised
217- # instead.
218- start_new_thread_returned .acquire ()
216+ start_joinable_thread_returned .acquire ()
219217 try :
220- thread . join_thread ( ident )
218+ handles [ 0 ]. join ( )
221219 except Exception as e :
222220 errors .append (e )
223221 finally :
224222 task_tried_to_join .release ()
225223
226224 with threading_helper .wait_threads_exit ():
227- joinable = True
228- ident = thread . start_new_thread ( task , (), {}, joinable )
229- start_new_thread_returned .release ()
230- # Can still join after join_thread() failed in other thread
225+ handle = thread . start_joinable_thread ( task )
226+ handles . append ( handle )
227+ start_joinable_thread_returned .release ()
228+ # Can still join after joining failed in other thread
231229 task_tried_to_join .acquire ()
232- thread . join_thread ( ident )
230+ handle . join ( )
233231
234232 assert len (errors ) == 1
235233 with self .assertRaisesRegex (RuntimeError , "Cannot join current thread" ):
236234 raise errors [0 ]
237235
238236 def test_detach_from_self (self ):
239237 errors = []
240- start_new_thread_returned = thread .allocate_lock ()
241- start_new_thread_returned .acquire ()
238+ handles = []
239+ start_joinable_thread_returned = thread .allocate_lock ()
240+ start_joinable_thread_returned .acquire ()
242241 thread_detached = thread .allocate_lock ()
243242 thread_detached .acquire ()
244243
245244 def task ():
246- ident = thread .get_ident ()
247- start_new_thread_returned .acquire ()
245+ start_joinable_thread_returned .acquire ()
248246 try :
249- thread . detach_thread ( ident )
247+ handles [ 0 ]. detach ( )
250248 except Exception as e :
251249 errors .append (e )
252250 finally :
253251 thread_detached .release ()
254252
255253 with threading_helper .wait_threads_exit ():
256- joinable = True
257- ident = thread . start_new_thread ( task , (), {}, joinable )
258- start_new_thread_returned .release ()
254+ handle = thread . start_joinable_thread ( task )
255+ handles . append ( handle )
256+ start_joinable_thread_returned .release ()
259257 thread_detached .acquire ()
260258 with self .assertRaisesRegex (ValueError , "not joinable" ):
261- thread . join_thread ( ident )
259+ handle . join ( )
262260
263261 assert len (errors ) == 0
264262
@@ -270,25 +268,23 @@ def task():
270268 lock .acquire ()
271269
272270 with threading_helper .wait_threads_exit ():
273- joinable = True
274- ident = thread .start_new_thread (task , (), {}, joinable )
275- # detach_thread() returns even though the thread is blocked on lock
276- thread .detach_thread (ident )
277- # join_thread() then cannot be called anymore
271+ handle = thread .start_joinable_thread (task )
272+ # detach() returns even though the thread is blocked on lock
273+ handle .detach ()
274+ # join() then cannot be called anymore
278275 with self .assertRaisesRegex (ValueError , "not joinable" ):
279- thread . join_thread ( ident )
276+ handle . join ( )
280277 lock .release ()
281278
282279 def test_join_then_detach (self ):
283280 def task ():
284281 pass
285282
286283 with threading_helper .wait_threads_exit ():
287- joinable = True
288- ident = thread .start_new_thread (task , (), {}, joinable )
289- thread .join_thread (ident )
284+ handle = thread .start_joinable_thread (task )
285+ handle .join ()
290286 with self .assertRaisesRegex (ValueError , "not joinable" ):
291- thread . detach_thread ( ident )
287+ handle . detach ( )
292288
293289
294290class Barrier :
0 commit comments