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;
}
using System.Collections.Concurrent;
using NUnit.Framework;
+using MonoTests.System.Threading.Tasks;
namespace MonoTests.System.Collections.Concurrent
{
CollectionStressTestHelper.RemoveStressTest (new ConcurrentQueue<int> (), CheckOrderingType.InOrder);
}
+ [Test]
+ public void StressTryPeekTestCase ()
+ {
+ ParallelTestHelper.Repeat (delegate {
+ var queue = new ConcurrentQueue<object> ();
+ queue.Enqueue (new object());
+
+ const int threads = 10;
+ int threadCounter = 0;
+ bool success = true;
+
+ ParallelTestHelper.ParallelStressTest (queue, (q) => {
+ int threadId = Interlocked.Increment (ref threadCounter);
+ object temp;
+ if (threadId < threads)
+ {
+ while (queue.TryPeek (out temp))
+ if (temp == null)
+ success = false;
+ } else {
+ queue.TryDequeue (out temp);
+ }
+ }, threads);
+
+ Assert.IsTrue (success, "TryPeek returned unexpected null value.");
+ }, 10);
+ }
+
[Test]
public void CountTestCase()
{