@@ -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,34 +338,49 @@ 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
+ int result = 0 ;
361
+ var wait = new Thread ( ( ) =>
357
362
{
363
+ int cnt = 0 ;
364
+ while ( cnt != th_cnt )
365
+ {
366
+ using ( Py . GIL ( ) )
367
+ {
368
+ cnt = ps . Get < int > ( "th_cnt" ) ;
369
+ }
370
+ Thread . Yield ( ) ;
371
+ }
372
+
358
373
using ( Py . GIL ( ) )
359
374
{
360
- cnt = ps . Get < int > ( "th_cnt " ) ;
375
+ result = ps . Get < int > ( "res " ) ;
361
376
}
362
- Thread . Yield ( ) ;
363
- }
364
- using ( Py . GIL ( ) )
377
+ } ) ;
378
+ wait . Start ( ) ;
379
+ if ( ! wait . Join ( timeout : TimeSpan . FromSeconds ( 30 ) ) )
365
380
{
366
- var result = ps . Get < int > ( "res" ) ;
367
- Assert . AreEqual ( 101 * th_cnt , result ) ;
381
+ Assert . Fail ( $ "started: { started } locked: { locked } unlocked: { unlocked } ") ;
368
382
}
383
+ Assert . AreEqual ( 101 * th_cnt , result ) ;
369
384
}
370
385
finally
371
386
{
0 commit comments