@@ -507,6 +507,9 @@ public ListContainer()
507
507
}
508
508
public UniqueList < string > List = new UniqueList < string > ( ) ;
509
509
public IEnumerable < string > SortedList => List . SortedValues ;
510
+
511
+ public long IterationCount = 0 ;
512
+ public long NonEmptyCount = 0 ;
510
513
}
511
514
512
515
/// <summary>
@@ -516,27 +519,32 @@ public ListContainer()
516
519
/// exception when multiple threads were accessing the property.
517
520
/// </summary>
518
521
[ Test ]
519
- public static void MultithreadEmptyValuesSorted ( )
522
+ [ TestCase ( "" ) ]
523
+ [ TestCase ( "Test" ) ]
524
+ public static void MultithreadEmptyValuesSorted ( string initialContent )
520
525
{
521
526
int nbrThreads = Environment . ProcessorCount ;
522
527
var container = new ListContainer ( ) ;
528
+ if ( ! string . IsNullOrEmpty ( initialContent ) )
529
+ container . List . Add ( initialContent ) ;
523
530
524
531
int TOTAL_TEST_COUNT = 100000 ;
525
532
long nbrThreadsFinished = 0 ;
526
533
long nbrThreadsGate1 = 0 ;
527
534
Exception taskTestException = null ;
528
535
529
536
// Note: Using a Barrier to synchronize all the threads at each iteration
530
- using ( Barrier barrier = new Barrier ( nbrThreads , ( b ) =>
537
+ using ( Barrier barrier = new Barrier ( 0 , ( b ) =>
531
538
{
539
+ container . List . SetDirty ( ) ;
532
540
Interlocked . Increment ( ref nbrThreadsGate1 ) ;
533
- container . List . AddRange ( new List < string > { } ) ; // Adding an empty collection makes the UniqueList dirty
534
541
} ) )
535
542
{
536
543
ThreadPool . TaskCallback taskLambda = ( object taskParams ) =>
537
544
{
538
545
var listContainersTask = ( ListContainer ) taskParams ;
539
-
546
+ int count = 0 ;
547
+ int nonEmptyCount = 0 ;
540
548
try
541
549
{
542
550
for ( int i = 0 ; i < TOTAL_TEST_COUNT ; ++ i )
@@ -547,11 +555,15 @@ public static void MultithreadEmptyValuesSorted()
547
555
if ( taskTestException != null )
548
556
break ; // Abort once we got an exception
549
557
550
- // Attempt to access the SortedList property from multiple threads.
551
- // It must not create any exception!
552
- foreach ( var s in container . SortedList )
558
+ for ( int j = 0 ; j < 5 ; ++ j )
553
559
{
554
- Console . WriteLine ( s ) ;
560
+ // Attempt to access the SortedList property from multiple threads.
561
+ // It must not create any exception!
562
+ foreach ( var s in container . SortedList )
563
+ {
564
+ ++ nonEmptyCount ;
565
+ }
566
+ ++ count ;
555
567
}
556
568
}
557
569
}
@@ -562,8 +574,10 @@ public static void MultithreadEmptyValuesSorted()
562
574
}
563
575
finally
564
576
{
565
- barrier . RemoveParticipant ( ) ; // Must remove the participant to unblock all the other threads.
566
577
Interlocked . Increment ( ref nbrThreadsFinished ) ;
578
+ Interlocked . Add ( ref listContainersTask . IterationCount , count ) ;
579
+ Interlocked . Add ( ref listContainersTask . IterationCount , nonEmptyCount ) ;
580
+ barrier . RemoveParticipant ( ) ;
567
581
}
568
582
} ;
569
583
@@ -572,6 +586,7 @@ public static void MultithreadEmptyValuesSorted()
572
586
{
573
587
// Add 1 task per thread
574
588
pool . Start ( nbrThreads ) ;
589
+ barrier . AddParticipants ( nbrThreads ) ;
575
590
for ( int i = 0 ; i < nbrThreads ; ++ i )
576
591
{
577
592
pool . AddTask ( taskLambda , container ) ;
@@ -582,6 +597,7 @@ public static void MultithreadEmptyValuesSorted()
582
597
583
598
// Check the results.
584
599
TestContext . Out . WriteLine ( "nbr Finished: {0}, nbr Gate1: {1}" , nbrThreadsFinished , nbrThreadsGate1 ) ;
600
+ TestContext . Out . WriteLine ( $ "IterationCount : { container . IterationCount } ") ;
585
601
if ( taskTestException != null )
586
602
{
587
603
throw taskTestException ;
0 commit comments