title: How can I use foreach and fork together to do something in parallel?
body: <p>This question is not UVM specific but the example that I am working on is UVM related.
  I have an array of agents in my UVM environment and I would like to launch a sequence on all of them in parallel.</p>
  
  <p>If I do the below:</p>
  
  <pre><code>foreach (env.agt[i])
    begin
      seq.start(env.agt[i].sqr);
    end
  </code></pre>
  
  <p>, the sequence <code>seq</code> first executes on <code>env.agt[0].sqr</code>. Once that gets over, it then executes on <code>env.agt[1].sqr</code> and so on.</p>
  
  <p>I want to implement a foreach-fork statement so that <code>seq</code> is executed in parallel on all <code>agt[i]</code> sequencers.</p>
  
  <p>No matter how I order the fork-join and foreach, I am not able to achieve that. Can you please help me get that parallel sequence launching behavior?</p>
  
  <p>Thanks.</p>
  
  <p>Update to clarify the problem I am trying to solve:
  The outcome of the below code constructs is the exact same as above without the fork-join.</p>
  
  <p><pre><code>
  foreach (env.agt[i])
    fork
      seq.start(env.agt[i].sqr);
    join
  </pre></code></p>
  
  <p><pre><code>
  fork
    foreach (env.agt[i])
      seq.start(env.agt[i].sqr);
    join
  </pre></code></p>
  
  <p><pre><code>
  // As per example in § 9.3.2 of IEEE SystemVerilog 2012 standard
  for (int i=0; i&lt;`CONST; ++i)
    begin
      fork
        automatic int var_i = i;
        seq.start(env.agt[var_i].sqr);
      join
    end
  </pre></code></p>