@@ -51,7 +51,7 @@ public void TestEval()
51
51
ps . Set ( "a" , 1 ) ;
52
52
var result = ps . Eval < int > ( "a + 2" ) ;
53
53
Assert . AreEqual ( 3 , result ) ;
54
- }
54
+ }
55
55
}
56
56
57
57
/// <summary>
@@ -286,7 +286,7 @@ public void TestImportScopeFunction()
286
286
public void TestVariables ( )
287
287
{
288
288
using ( Py . GIL ( ) )
289
- {
289
+ {
290
290
( ps . Variables ( ) as dynamic ) [ "ee" ] = new PyInt ( 200 ) ;
291
291
var a0 = ps . Get < int > ( "ee" ) ;
292
292
Assert . AreEqual ( 200 , a0 ) ;
@@ -338,28 +338,41 @@ public void TestThread()
338
338
" th_cnt += 1\n "
339
339
) ;
340
340
}
341
- int th_cnt = 100 ;
341
+ const int th_cnt = 100 ;
342
+ int locked = 0 , unlocked = 0 , started = 0 ;
342
343
for ( int i = 0 ; i < th_cnt ; i ++ )
343
344
{
344
- System . Threading . Thread th = new System . Threading . Thread ( ( ) =>
345
+ ThreadPool . QueueUserWorkItem ( _ =>
346
+ // var th = new System.Threading.Thread(() =>
345
347
{
348
+ Interlocked . Increment ( ref started ) ;
346
349
using ( Py . GIL ( ) )
347
350
{
351
+ Interlocked . Increment ( ref locked ) ;
348
352
//ps.GetVariable<dynamic>("update")(); //call the scope function dynamicly
349
353
_ps . update ( ) ;
350
354
}
355
+ Interlocked . Increment ( ref unlocked ) ;
351
356
} ) ;
352
- th . Start ( ) ;
357
+ // th.Start();
353
358
}
354
359
//equivalent to Thread.Join, make the main thread join the GIL competition
355
- int cnt = 0 ;
356
- while ( cnt != th_cnt )
360
+ var wait = new Thread ( ( ) =>
357
361
{
358
- using ( Py . GIL ( ) )
362
+ int cnt = 0 ;
363
+ while ( cnt != th_cnt )
359
364
{
360
- cnt = ps . Get < int > ( "th_cnt" ) ;
365
+ using ( Py . GIL ( ) )
366
+ {
367
+ cnt = ps . Get < int > ( "th_cnt" ) ;
368
+ }
369
+ Thread . Yield ( ) ;
361
370
}
362
- Thread . Yield ( ) ;
371
+ } ) ;
372
+ wait . Start ( ) ;
373
+ if ( ! wait . Join ( timeout : TimeSpan . FromSeconds ( 30 ) ) )
374
+ {
375
+ Assert . Fail ( $ "started: { started } locked: { locked } unlocked: { unlocked } ") ;
363
376
}
364
377
using ( Py . GIL ( ) )
365
378
{
0 commit comments