Fixed rare index bug when using multiple multithreading consumers

This commit is contained in:
Thorsten Sommer 2023-11-14 15:38:41 +01:00
parent faf6c99c19
commit a838dacf2a
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5

View File

@ -295,6 +295,11 @@ public sealed class MultiThreadedRng<TNum> : IRandom<TNum>, IDisposable where TN
// Made a local copy of the current pointer: // Made a local copy of the current pointer:
var myPointer = this.currentBufferPointer; 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: // Increment the pointer for the next thread or call:
var nextPointer = myPointer + 1; var nextPointer = myPointer + 1;
@ -303,7 +308,7 @@ public sealed class MultiThreadedRng<TNum> : IRandom<TNum>, IDisposable where TN
goto Start; 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]; return this.currentBuffer[myPointer];
} }