diff --git a/src/tests/GC/Scenarios/THDChaos/livingobject.cs b/src/tests/GC/Scenarios/THDChaos/livingobject.cs index d88dd0c4fb0595..e01b0b06aa4bd4 100644 --- a/src/tests/GC/Scenarios/THDChaos/livingobject.cs +++ b/src/tests/GC/Scenarios/THDChaos/livingobject.cs @@ -24,7 +24,7 @@ public void ThreadStart( ) { // console synchronization Console.SetOut(TextWriter.Synchronized(Console.Out)); - if( iCounter%100 == 0) + if( Volatile.Read( ref iCounter )%100 == 0) { Console.Out.WriteLine( iCounter + " number of threads has been started" ); } @@ -43,9 +43,9 @@ public void ThreadStart( ) MethodContainer[ 0 ] = ( byte ) 1; MethodContainer[ MethodContainer.Length - 1 ] = ( byte ) 1; - IncreatCount( ); - - if( LivingObject.iCounter < ThdChaos.iThrd ) + // Atomically reserve a thread creation slot before starting a successor thread, + // so that no more than ThdChaos.iThrd successor threads are ever created. + if( IncreatCount( ) <= ThdChaos.iThrd ) { Thread Mv_Thread = new Thread( new ThreadStart (this.ThreadStart) ); Mv_Thread.Start( ); @@ -54,12 +54,9 @@ public void ThreadStart( ) } - public void IncreatCount() + public int IncreatCount() { - lock(this) - { - iCounter += 1; - } + return Interlocked.Increment( ref iCounter ); } } diff --git a/src/tests/GC/Scenarios/THDChaos/thdchaos.cs b/src/tests/GC/Scenarios/THDChaos/thdchaos.cs index 44fa03c9837f96..6df7f458cfb721 100644 --- a/src/tests/GC/Scenarios/THDChaos/thdchaos.cs +++ b/src/tests/GC/Scenarios/THDChaos/thdchaos.cs @@ -12,6 +12,7 @@ namespace DefaultNamespace { using System.Threading; using System; using System.IO; + using System.Runtime.InteropServices; using TestLibrary; public class ThdChaos @@ -40,6 +41,13 @@ public static int Main( System.String [] Args ) iThrd = 20; } + // 32-bit Arm has a limited address space, creating too many threads at once + // can exhaust it and result in OutOfMemoryException. + if( RuntimeInformation.ProcessArchitecture == Architecture.Arm && iThrd > 5 ) + { + iThrd = 5; + } + ThdChaos Mv_ThdChaos = new ThdChaos(); MasterThread Mv_Thread = new MasterThread( iThrd ); return 100;