Resolve "Bug: IndexOutOfRangeException when using multi-threaded consumers" #16

Merged
thorsten merged 3 commits from 8-bug-indexoutofrangeexception-when-using-multi-threaded-consumers into main 2023-11-14 14:43:59 +00:00
Showing only changes of commit a838dacf2a - Show all commits

View File

@ -295,6 +295,11 @@ public sealed class MultiThreadedRng<TNum> : IRandom<TNum>, 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<TNum> : IRandom<TNum>, 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];
}