Reputation: 5072
Always been a tad weak around with threads and just going through Steven Toub's Parallel Computing book.
On Page 39 there are examples of Fork/Join patterns below
static T[] MyParallelInvoke<T>(params Func<T>[] functions)
{
T[] results = new T[functions.Length];
Parallel.For(0, functions.Length, i =>
{
results[i] = functions[i]();
});
return results;
}
// Approach #4: Using PLINQ
static T[] MyParallelInvoke<T>(params Func<T>[] functions)
{
return functions.AsParallel().Select(f => f()).ToArray();
}
For Approach 3 for example above just to clarify do the results all offically have values, when you do
"return results"?
Or will only some of them have values depending on if the Thread has been completed or not?
Similarly for Approach 4 when you call ToArray()
Upvotes: 2
Views: 4663
Reputation: 273314
Yes, in both case there is an implicit WaitAll()
.
The code outside Parallel.For()
is single-threaded.
Upvotes: 2
Reputation: 410
Parallel.For() will return a ParallelLoopResult that has an IsCompleted property.
Upvotes: 1