From a838dacf2a43cff2efd8f814dff0b74cb06c6f2e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 14 Nov 2023 15:38:41 +0100 Subject: [PATCH] Fixed rare index bug when using multiple multithreading consumers --- FastRng/MultiThreadedRng.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/FastRng/MultiThreadedRng.cs b/FastRng/MultiThreadedRng.cs index a965bb8..68c2177 100644 --- a/FastRng/MultiThreadedRng.cs +++ b/FastRng/MultiThreadedRng.cs @@ -295,6 +295,11 @@ public sealed class MultiThreadedRng : IRandom, IDisposable where TN // Made a local copy of the current pointer: var myPointer = this.currentBufferPointer; + // Issue #8: This might happens when the current thread was interrupted by another thread, and + // the other thread has already updated the pointer. In this case, we start over again. + if (myPointer >= BUFFER_SIZE) + goto Start; + // Increment the pointer for the next thread or call: var nextPointer = myPointer + 1; @@ -303,7 +308,7 @@ public sealed class MultiThreadedRng : IRandom, IDisposable where TN goto Start; // - // Case: Success. We updated the pointer and, thus, can use the read number. + // Case: Success. We updated the pointer and, thus, can use it to read the number. // return this.currentBuffer[myPointer]; }