Add start gate to by-divisor task scheduling

This commit is contained in:
Marek Lemańczyk committed 2025-10-28 15:32:15 +01:00
1 parent 29f58e3059
commit c4c783610b
1 file changed
+5
@@ -278,6 +278,7 @@ public static class MersenneNumberDivisorByDivisorTester
int taskCount = (totalCount + partitionSize - 1) / partitionSize; int taskCount = (totalCount + partitionSize - 1) / partitionSize;
Task[] tasks = new Task[taskCount]; Task[] tasks = new Task[taskCount];
var startGate = new ManualResetEventSlim(initialState: false);
int taskIndex = 0; int taskIndex = 0;
for (int start = 0; start < totalCount; start += partitionSize) for (int start = 0; start < totalCount; start += partitionSize)
@@ -292,6 +293,8 @@ public static class MersenneNumberDivisorByDivisorTester
tasks[taskIndex++] = Task.Factory.StartNew( tasks[taskIndex++] = Task.Factory.StartNew(
() => () =>
{ {
startGate.Wait();
for (int index = rangeStart; index < rangeEnd; index++) for (int index = rangeStart; index < rangeEnd; index++)
{ {
ProcessPrime(filteredPrimes[index]); ProcessPrime(filteredPrimes[index]);
@@ -302,7 +305,9 @@ public static class MersenneNumberDivisorByDivisorTester
scheduler); scheduler);
} }
startGate.Set();
Task.WaitAll(tasks); Task.WaitAll(tasks);
startGate.Dispose();
} }
} }
} }