From faf6c99c195c70a3e82754bff6fb1644c99f4b74 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 14 Nov 2023 13:55:48 +0100 Subject: [PATCH 1/3] Added unit test to cover issue #8 --- FastRngTests/MultiThreadedRngTests.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/FastRngTests/MultiThreadedRngTests.cs b/FastRngTests/MultiThreadedRngTests.cs index ae9cc83..f00869c 100644 --- a/FastRngTests/MultiThreadedRngTests.cs +++ b/FastRngTests/MultiThreadedRngTests.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Threading; +using System.Threading.Tasks; using FastRng; using FastRng.Distributions; using NUnit.Framework; @@ -222,6 +223,32 @@ public class MultiThreadedRngTests Assert.That(lorentzContains0, Is.False, "Lorentz distribution contained 0"); Assert.That(lorentzContains1, Is.True, "Lorentz distribution does not contained 1"); } + + [Test] + [Category(TestCategories.LONG_RUNNING)] + public void TestMultiThreadedConsumer() + { + var job1 = Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning); + var job2 = Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning); + var job3 = Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning); + + Assert.DoesNotThrowAsync(async () => await job1); + Assert.DoesNotThrowAsync(async () => await job2); + Assert.DoesNotThrowAsync(async () => await job3); + return; + + float Run() + { + var sum = 0f; + var distLorentz = new CauchyLorentzX1(this.rng); + for (int i = 0; i < 100_000_000; i++) + { + sum += distLorentz.NextNumber(); + } + + return sum; + } + } [Test] [Category(TestCategories.COVER)] -- 2.43.0 From a838dacf2a43cff2efd8f814dff0b74cb06c6f2e Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 14 Nov 2023 15:38:41 +0100 Subject: [PATCH 2/3] 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]; } -- 2.43.0 From f1fe1de0903b56da5edba474a5491fa1f07cf161 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Tue, 14 Nov 2023 15:43:11 +0100 Subject: [PATCH 3/3] Increment version --- FastRng/FastRng.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FastRng/FastRng.csproj b/FastRng/FastRng.csproj index 6487be4..74eaaf3 100644 --- a/FastRng/FastRng.csproj +++ b/FastRng/FastRng.csproj @@ -3,9 +3,9 @@ net7.0 true - 1.1.1 - 1.1.1 - 1.1.1 + 1.1.2 + 1.1.2 + 1.1.2 Thorsten Sommer https://devops.tsommer.org/open-source/dotnet/FastRng https://devops.tsommer.org/open-source/dotnet/FastRng -- 2.43.0