What's the best way of achieving a parallel infinite Loop?

redcalx picture redcalx · Dec 29, 2011 · Viewed 7.1k times · Source

I've gotten used to using Parallel.For() in .Net's parallel extensions as it's a simple way of parallelizing code without having to manually start and maintain threads (which can be fiddly). I'm now looking at an infinite loop (do something until I signal it to stop) that I wish to parallelize, there isn't an argument free Parallel.For() overload to do this so was wondering what the best approach here would be. In principle I could just do something like:

Parallel.For(0, int.Max)

But I'm suspecting that might not be an expected/efficient pattern for the work partitioning logic to handle(?)

Another option is something like:

for(;;)
{
    Parallel.For(0, 128, delegate()
    {
       // Do stuff.
    }
}

But that seems inelegant and may also result in inefficient work partitioning.

Right now my instinct is to do this manually by creating and maintaining my own threads, but I would be interested in getting some feedback/opinions on this. Thanks.

=== UPDATE ===

I'm using a simplified version of the code from the article in the accepted answer (I've removed the ParallelOptions parameter). Here's the code...

public class ParallelUtils
{
    public static void While(Func<bool> condition, Action body) 
    { 
        Parallel.ForEach(IterateUntilFalse(condition), ignored => body()); 
    }

    private static IEnumerable<bool> IterateUntilFalse(Func<bool> condition) 
    { 
        while (condition()) yield return true; 
    }
}

An example usage would be:

Func<bool> whileCondFn = () => !_requestStopFlag;
ParallelUtils.While(whileCondFn, delegate()
{
    // Do stuff.


});

Answer

Rynant picture Rynant · Dec 29, 2011

Stephen Toub has a post about Implementing Parallel While with Parallel.ForEach.