@@ -165,100 +165,98 @@ def test_join_thread(self):
165
165
166
166
def task ():
167
167
time .sleep (0.05 )
168
- finished .append (None )
168
+ finished .append (thread . get_ident () )
169
169
170
170
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 ()
174
173
self .assertEqual (len (finished ), 1 )
174
+ self .assertEqual (handle .ident , finished [0 ])
175
175
176
176
def test_join_thread_already_exited (self ):
177
177
def task ():
178
178
pass
179
179
180
180
with threading_helper .wait_threads_exit ():
181
- joinable = True
182
- ident = thread .start_new_thread (task , (), {}, joinable )
181
+ handle = thread .start_joinable_thread (task )
183
182
time .sleep (0.05 )
184
- thread . join_thread ( ident )
183
+ handle . join ( )
185
184
186
- def test_join_non_joinable (self ):
185
+ def test_join_several_times (self ):
187
186
def task ():
188
187
pass
189
188
190
189
with threading_helper .wait_threads_exit ():
191
- ident = thread .start_new_thread (task , ())
190
+ handle = thread .start_joinable_thread (task )
191
+ handle .join ()
192
192
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 ()
194
198
195
- def test_join_several_times (self ):
196
199
def task ():
197
- pass
200
+ handle_destroyed . acquire ()
198
201
199
202
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 ()
205
206
206
207
def test_join_from_self (self ):
207
208
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 ()
210
212
task_tried_to_join = thread .allocate_lock ()
211
213
task_tried_to_join .acquire ()
212
214
213
215
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 ()
219
217
try :
220
- thread . join_thread ( ident )
218
+ handles [ 0 ]. join ( )
221
219
except Exception as e :
222
220
errors .append (e )
223
221
finally :
224
222
task_tried_to_join .release ()
225
223
226
224
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
231
229
task_tried_to_join .acquire ()
232
- thread . join_thread ( ident )
230
+ handle . join ( )
233
231
234
232
assert len (errors ) == 1
235
233
with self .assertRaisesRegex (RuntimeError , "Cannot join current thread" ):
236
234
raise errors [0 ]
237
235
238
236
def test_detach_from_self (self ):
239
237
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 ()
242
241
thread_detached = thread .allocate_lock ()
243
242
thread_detached .acquire ()
244
243
245
244
def task ():
246
- ident = thread .get_ident ()
247
- start_new_thread_returned .acquire ()
245
+ start_joinable_thread_returned .acquire ()
248
246
try :
249
- thread . detach_thread ( ident )
247
+ handles [ 0 ]. detach ( )
250
248
except Exception as e :
251
249
errors .append (e )
252
250
finally :
253
251
thread_detached .release ()
254
252
255
253
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 ()
259
257
thread_detached .acquire ()
260
258
with self .assertRaisesRegex (ValueError , "not joinable" ):
261
- thread . join_thread ( ident )
259
+ handle . join ( )
262
260
263
261
assert len (errors ) == 0
264
262
@@ -270,25 +268,23 @@ def task():
270
268
lock .acquire ()
271
269
272
270
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
278
275
with self .assertRaisesRegex (ValueError , "not joinable" ):
279
- thread . join_thread ( ident )
276
+ handle . join ( )
280
277
lock .release ()
281
278
282
279
def test_join_then_detach (self ):
283
280
def task ():
284
281
pass
285
282
286
283
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 ()
290
286
with self .assertRaisesRegex (ValueError , "not joinable" ):
291
- thread . detach_thread ( ident )
287
+ handle . detach ( )
292
288
293
289
294
290
class Barrier :
0 commit comments