The speculative processing pattern is another parallel programming pattern that relies on high throughput to reduce latency. This is very useful in scenarios where there are multiple ways of performing a task but the user doesn't know which way will return the results fastest. This approach creates a task for each possible method, which is then executed across processors. The task that finishes first is used as output, ignoring the others (which may still complete successfully but are slow).
The following is a typical SpeculativeInvoke representation. It accepts an array of Func<T> as parameters and executes them in parallel until one of them returns:
public static T SpeculativeInvoke<T>(params Func<T>[] functions)
{
return SpeculativeForEach(functions, function => function());
}
The following method executes each action...