title: Is there a way to change how Parallel.Foreach interlaces an array that its operating on?
body: <p>I'm using <code>Parallel.Foreach</code> to operate over a large array where each array element corresponds to a line in a bitmap image. However, <code>Parallel.Foreach</code> appears to allocate a fix number of threads, say 6 threads, and gives 1/6 of the array to the first thread, and 1/6 of the array to the next thread in order.  example:</p>
  
  <pre><code>[0]=&gt;thread1, [1]=&gt;thread1, [2]=&gt;thread1, [3]=&gt;thread1
  
  [4]=&gt;thread2, [5]=&gt;thread2, [6]=&gt;thread2, [7]=&gt;thread2,
  
  .. and so forth
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/sjtmA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sjtmA.png" alt="enter image description here"></a></p>
  
  <p>roughly speaking, want I want is an interlace pattern that alternates between threads for each increment of array index...for example:</p>
  
  <pre><code>[0]=&gt;thread1 [1]=&gt;thread2 [2]=&gt;thread3 [4]=&gt;thread4, [5]=&gt;thread1, [6]=&gt;thread2, etc...
  </code></pre>
  
  <p>is there a way to change the interlace pattern that Parallel.Foreach assigns to each parallel thread actively executing?</p>
  
  <p>I'm trying to get the image to render using a gif interlaced pattern rather than having the bitmap lines rendered sequential in 6 different lines on the image that fill downwards...</p>
  
  <pre><code>using System.Threading.Tasks;
  using System.Threading;
  using System.Collections.Concurrent;
  
  //…
  
  void button_click() {
      Task.Run(() =&gt;{
          start_task();
      });
  }
  
  int ds_height=600;
  public bool start_task()
  {
      var cpu75 = Convert.ToInt32(Math.Ceiling((
             Environment.ProcessorCount * 0.75) * 1.0));
      //MandelInit();
  
      int[] ytmp;
  
      int version = 2;
      if (version == 1) {
          ytmp = MandelYLace();
      }
      else {
          int[] ytmp = new int[ds_height];
          for(int i=0; i &lt; ds_height; i++)
          {
              ytmp[i] = i;
          }
          Parallel.ForEach(
              ytmp, //ylace,
              new ParallelOptions { MaxDegreeOfParallelism = cpu75 },
              yy =&gt; {
                  //ybuff[yy] = MandelLine(yy);
                  //ydone.Enqueue(yy);
              }
          );
          stop = true;
          return true;
  }
  
  // Interlace Y-Lines using GIF Interlaced method
  int[] MandelYLace()
  {
      var ylace = new int[ds_height];
      for (int y = 0, yg=0, yy=0; y &lt; ds_height; y++)
      {
          ylace[y] = yy;
  
          if (yg == 0 || yg == 1)
              yy += 8;
          else if (yg == 2)
              yy += 4;
          else if (yg == 3)
              yy += 2;
  
          if (yy &gt;= ds_height)
          {
              yg = (yg + 1) % 4;
              if (yg == 1) yy = 4;
              else if (yg == 2) yy = 2;
              else if (yg == 3) yy = 1; 
              else if (yg == 0) yy = 0; 
          }
      }
  
      return ylace;
  }
  </code></pre>