PLINQ delayed execution PLINQ delayed execution multithreading multithreading

PLINQ delayed execution


Unfortunately, the other threads will not "yield" immediately.

As soon as Any() finds a valid element, the PLINQ scheduler will stop scheduling new threads to check for new elements. Any existing partitioners will also receive a cancellation request, which will prevent those partitions from calling Any() on another item.

However, any threads that are currently executing the lambda expression within your Any() method will still be executing, as there's no way for them to know that another thread has succeeded. It will prevent new threads from calling into Any(), but not cancel all of the ones in a "very expensive" delegate.

On a side note:

PLINQ, unlike LINQ to Objects, doesn't really use deferred execution. When you call AsParallel() on an IEnumerable<T>, the ParallelQuery<T> that is generated will actually start processing your routine in parallel. Deferred execution would dramatically reduce the effectiveness of PLINQ, since it would be impossible to schedule, in parallel, without creating the work partitioners and scheduling in advance.


Edit:

After thinking about this - if your lambda is VERY expensive, you might want to consider using a CancellationToken. I blogged, in detail, about how cancellation in PLINQ works. Typically, you'd just use a token and call ThrowIfCancellationRequested() - however, you can also use a CancellationToken and check IsCancellationRequested, which would let you make your lambda "exit early", providing you a way to stop the background processing sooner...