From 4449269d8fa64d0bdb09908ae28a8cc020989326 Mon Sep 17 00:00:00 2001 From: marcos henrich Date: Fri, 30 May 2014 20:31:47 +0100 Subject: [PATCH] Changed TryPeek to handle race condition with Dequeue. Dequeue sets the head.Value to its default. When TryPeek is interrupted in the process of retrieving the value some times Dequeue sets the value to its default. TryPeek now checks the head has not been changed before returning the value. --- .../ConcurrentQueue.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/mcs/class/corlib/System.Collections.Concurrent/ConcurrentQueue.cs b/mcs/class/corlib/System.Collections.Concurrent/ConcurrentQueue.cs index aff2492e021..ea8984394b4 100644 --- a/mcs/class/corlib/System.Collections.Concurrent/ConcurrentQueue.cs +++ b/mcs/class/corlib/System.Collections.Concurrent/ConcurrentQueue.cs @@ -132,14 +132,24 @@ namespace System.Collections.Concurrent public bool TryPeek (out T result) { - Node first = head.Next; + result = default (T); + bool update = true; + + while (update) + { + Node oldHead = head; + Node oldNext = oldHead.Next; - if (first == null) { - result = default (T); - return false; - } + if (oldNext == null) { + result = default (T); + return false; + } - result = first.Value; + result = oldNext.Value; + + //check if head has been updated + update = head != oldHead; + } return true; } -- 2.25.1