PLINQ behaves differently in .NET 8

92 views Asked by At

Run the following code:

using System;
using static System.Console;
var numbers = Enumerable.Range(0, 31);

WriteLine("\nNumbers divisible by 5 are(using LINQ):");
numbers
.Where(num => num % 5 == 0)
.ToList()
.ForEach(num => Write($"{num}\t"));

WriteLine("\nNumbers divisible by 5 are(using PLINQ):");
numbers
.AsParallel()
.Where(num => num % 5 == 0)
.ToList()
.ForEach(num => Write($"{num}\t"));


WriteLine("\nNumbers divisible by 5 are(using PLINQ with forcing parallelism):");
numbers
.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.Where(num => num % 5 == 0)
.ToList()
.ForEach(num => Write($"{num}\t"));

Here is a sample output from .NET 7( Expected Behavior):

Numbers divisible by 5 are(using LINQ):
0       5       10      15      20      25      30
Numbers divisible by 5 are(using PLINQ):
5       20      25      0       30      15      10
Numbers divisible by 5 are(using PLINQ with forcing parallelism):
25      30      0       5       10      15      20

Now execute the same code in .NET 8: Everytime I see the following output:

Numbers divisible by 5 are(using LINQ):
0       5       10      15      20      25      30
Numbers divisible by 5 are(using PLINQ):
0       5       10      15      20      25      30
Numbers divisible by 5 are(using PLINQ with forcing parallelism):
0       5       10      15      20      25      30

This is why it appears to me that the PLINQ engine is broken in .NET 8. Please correct me if I missed something here.

You can see that the PLINQ behavior in .NET 7 and .NET8 are different. In fact, .NET8 behavior is not a expected behavior.Any thought will be appretiated.

0

There are 0 answers