dimanche 28 septembre 2014

Parallel.For and Task.CurrentID strange behavior


Vote count:

0




I've discovered the following strange behavior for Parallel.For when option MaxDegreeOfParallelism is set.


I have the following program.



using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace CurrentId
{
internal class Program
{
private static void Main(string[] args)
{

ParallelOptions po = new ParallelOptions();
po.MaxDegreeOfParallelism = 3;

Console.WriteLine("Parallel.For without delay.");
Parallel.For(0, 19, po, i =>
{
Console.WriteLine("{0} on Task {1}", i, Task.CurrentId);
});

Console.WriteLine("Parallel.For with delay - Thread.Sleep.");
Parallel.For(0, 19, po, i =>
{
Thread.Sleep(100);
Console.WriteLine("{0} on Task {1}", i, Task.CurrentId);
});

Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
}


While executing a program I get the following output. As you can see, when first Parallel.For is executing there are only three different kind task id = 1,2,3. But when the second Parallel.For is executing there are SIX different kind task id = 3,4,5,6,7,8. As I suppose the maximum count of different kind of task must correspond to the MaxDegreeOfParallelism option. I don't know how to deal with it. Any ideas?



asked 22 secs ago







Parallel.For and Task.CurrentID strange behavior

Aucun commentaire:

Enregistrer un commentaire