PLINQ delays execution

I am trying to understand how parallelism can work using PLINQ, given the delay in execution. Here is a simple example.

string[] words = { "believe", "receipt", "relief", "field" };
bool result = words.AsParallel().Any(w => w.Contains("ei"));

With LINQ, I expect the execution to reach the β€œget” value and return true without executing the query for the rest of the values.

If we do this in parallel, the assessment of β€œrelief” may begin before the result of β€œreceiving” returns. But as soon as the request finds out that the "receipt" will lead to a true result, will the current flows be immediate?

In my case, this is important, because β€œany” test can be very expensive, and I would like to free processors for other tasks.

+3
source share
1 answer

Unfortunately, other threads will not be issued immediately.

Once it Any()finds a valid item, the PLINQ scheduler will stop scheduling new threads to check for new items. Any existing delimiters will also receive a revocation request, which will prevent these sections from accessing Any()another element.

However, all threads that currently execute the lambda expression inside your method Any()will execute, as there is no way to know that another thread has succeeded. This will prevent new threads from being called in Any(), but will not undo all of them in "very expensive" deletes.

On a side note:

PLINQ, LINQ to Objects, . AsParallel() IEnumerable<T>, ParallelQuery<T> . PLINQ, , .


Edit:

- , CancellationToken. , PLINQ. , ThrowIfCancellationRequested() - CancellationToken IsCancellationRequested, " ", ...

+4

Source: https://habr.com/ru/post/1735982/


All Articles