C # System.Threading.Tasks.Parallel.For en ulong
-
28-09-2019 - |
Pregunta
En C #, hay un System.Threading.Tasks.Parallel.For (...) wich hace lo mismo que una orden para-loop, sin, pero en varios subprocesos. La cuestión es que sólo funciona en tiempo y int, yo quiero trabajar con ulong. Está bien, puedo encasillado pero tengo algunos problemas con las fronteras. Vamos a decir, quiero un bucle de long.MaxValue-10 a long.MaxValue + 10 (recuerden, estoy hablando de ulong) ... ¿cómo lo hago? (Debo admitir que me siento un poco estúpida en este momento, pero es que no puedo con ello en este momento)
Un ejemplo:
for (long i = long.MaxValue - 10; i < long.MaxValue; ++i)
{
Console.WriteLine(i);
}
//does the same as
System.Threading.Tasks.Parallel.For(long.MaxValue - 10, long.MaxValue, delegate(long i)
{
Console.WriteLine(i);
});
//except for the order, but theres no equivalent for
long max = long.MaxValue;
for (ulong i = (ulong)max - 10; i < (ulong)max + 10; ++i)
{
Console.WriteLine(i);
}
Solución
Siempre se puede escribir en Microsoft y pedirles que añadir Parallel.For (ulong, ulong, Acción
Parallel.For(-10L, 10L, x => { var index = long.MaxValue + (ulong) x; });
Otros consejos
o puede crear una gama personalizada para Parallel.ForEach
public static IEnumerable<ulong> Range(ulong fromInclusive, ulong toExclusive)
{
for (var i = fromInclusive; i < toExclusive; i++) yield return i;
}
public static void ParallelFor(ulong fromInclusive, ulong toExclusive, Action<ulong> body)
{
Parallel.ForEach(
Range(fromInclusive, toExclusive),
new ParallelOptions { MaxDegreeOfParallelism = 4 },
body);
}