none
Parallel.Foreach and yield return

    Question

  • I want to do something like this:

    foreach (var e in source)
       yield return e;

    with a Parallel.Foreach?

    Is it possible and if yes, how

    Friday, September 04, 2009 7:15 PM

Answers

  • You'll need to make the enumerator separate from your loop, but process it in parallel.  Yield return will return a value of the enumerable collection - this wouldn't make sense, because one object would be using the enumeration NOT in parallel, but trying to return parallelized...

    The normal way you'd do this is like:

    // In your source, you use yield
    public ClassImplementingIEnumerable: IEnumerable<int>
    {
         public IEnumerable<int> GetSource()
         {
               for (int i=0;i<1000;++i)
                   yield return i;
         }
    }


    public class ParallelProcessingConsumer {

        public void SomeMethod(ClassImplementingIEnumerable sourceProvider)
        {
          
              Parallel.ForEach(sourceProvider.GetSource(), parallelOptions, (i, loopState) =>
              { 
                 // Do work in parallel!
              });
        }



    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, September 04, 2009 7:28 PM
    Moderator

All replies

  • You'll need to make the enumerator separate from your loop, but process it in parallel.  Yield return will return a value of the enumerable collection - this wouldn't make sense, because one object would be using the enumeration NOT in parallel, but trying to return parallelized...

    The normal way you'd do this is like:

    // In your source, you use yield
    public ClassImplementingIEnumerable: IEnumerable<int>
    {
         public IEnumerable<int> GetSource()
         {
               for (int i=0;i<1000;++i)
                   yield return i;
         }
    }


    public class ParallelProcessingConsumer {

        public void SomeMethod(ClassImplementingIEnumerable sourceProvider)
        {
          
              Parallel.ForEach(sourceProvider.GetSource(), parallelOptions, (i, loopState) =>
              { 
                 // Do work in parallel!
              });
        }



    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, September 04, 2009 7:28 PM
    Moderator
  • Matthieu, are you looking to consume an iterator with a Parallel.ForEach, or implement an iterator with a Parallel.ForEach?  Reed's answer addresses the former.
    Monday, September 07, 2009 7:30 PM
    Owner