diff --git a/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueueSegment.cs b/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueueSegment.cs index c706fae02e1..85224a537a5 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueueSegment.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueueSegment.cs @@ -112,15 +112,15 @@ internal static int RoundUpToPowerOf2(int i) _frozenForEnqueues = true; // Increase the tail by FreezeOffset, spinning until we're successful in doing so. - var spinner = new SpinWait(); + int tail = _headAndTail.Tail; while (true) { - int tail = Volatile.Read(ref _headAndTail.Tail); - if (Interlocked.CompareExchange(ref _headAndTail.Tail, tail + FreezeOffset, tail) == tail) + int oldTail = Interlocked.CompareExchange(ref _headAndTail.Tail, tail + FreezeOffset, tail); + if (oldTail == tail) { break; } - spinner.SpinOnce(); + tail = oldTail; } } }