Fixed multi-threading access to random

This commit is contained in:
Thorsten Sommer 2020-09-24 20:22:05 +02:00
parent 083c274818
commit e43738d976

View File

@ -15,6 +15,8 @@ namespace FastRng
private const int CAPACITY_RANDOM_NUMBERS_4_SOURCE = 16_000_000; private const int CAPACITY_RANDOM_NUMBERS_4_SOURCE = 16_000_000;
#endif #endif
private static readonly object SYNC = new object();
private readonly System.Random rng = new System.Random(); private readonly System.Random rng = new System.Random();
private readonly CancellationTokenSource producerToken = new CancellationTokenSource(); private readonly CancellationTokenSource producerToken = new CancellationTokenSource();
@ -63,6 +65,14 @@ namespace FastRng
// //
var buffer = new double[CAPACITY_RANDOM_NUMBERS_4_SOURCE]; var buffer = new double[CAPACITY_RANDOM_NUMBERS_4_SOURCE];
//
// Random is not thread-safe!
// Because we using two threads, we ensure that one threads generates
// next bag of numbers while the other pumps its numbers into the channel.
//
lock (SYNC)
{
for (var n = 0; n < buffer.Length && !cancellationToken.IsCancellationRequested; n++) for (var n = 0; n < buffer.Length && !cancellationToken.IsCancellationRequested; n++)
{ {
#region Re-implementation of GetSampleForLargeRange() method of .NET #region Re-implementation of GetSampleForLargeRange() method of .NET
@ -80,6 +90,7 @@ namespace FastRng
buffer[n] = d; buffer[n] = d;
} }
}
for (var n = 0; n < buffer.Length && !cancellationToken.IsCancellationRequested; n++) for (var n = 0; n < buffer.Length && !cancellationToken.IsCancellationRequested; n++)
await channelWriter.WriteAsync(buffer[n], cancellationToken); await channelWriter.WriteAsync(buffer[n], cancellationToken);