title: systemVerilog Nested Fork
body: <p>I came across code written by someone else, and I don't understand how it works?</p>
  
  <pre><code> // Task A
   task sub_run_a();
      while ($time &lt; 50us) begin
      #1us;
      $display("sub_run_a(): ping at time %d", $time);
    end
  endtask : sub_run_a
  
  // Task B
  task sub_run_b();
    #5us;
    $display("sub_run_b() finished");
  endtask : sub_run_b
  
  // Task C
  task sub_run_c();
    #10us;
    $display("sub_run_c() finished");
  endtask : sub_run_c
  </code></pre>
  
  <p>And this is how the testbench is setup:</p>
  
  <pre><code>fork 
    fork
      sub_run_c();  
      sub_run_b();
    join
      sub_run_a();
  join_any
  </code></pre>
  
  <p>When I look at the simulation result, it seems that all the tasks run in parallel, and I don't understand what is going on.</p>
  
  <p>Isn't it that Task A should NOT start until task B and Task C are done?</p>
  
  <p>But, this is not the case, since this is the output:</p>
  
  <pre><code># KERNEL: sub_run_a(): ping at time                 1000
  # KERNEL: sub_run_a(): ping at time                 2000
  # KERNEL: sub_run_a(): ping at time                 3000
  # KERNEL: sub_run_a(): ping at time                 4000
  # KERNEL: sub_run_b() finished
  # KERNEL: sub_run_a(): ping at time                 5000
  # KERNEL: sub_run_a(): ping at time                 6000
  # KERNEL: sub_run_a(): ping at time                 7000
  # KERNEL: sub_run_a(): ping at time                 8000
  # KERNEL: sub_run_a(): ping at time                 9000
  # KERNEL: sub_run_c() finished
  # KERNEL: sub_run_a(): ping at time                10000
  # KERNEL: sub_run_a(): ping at time                11000
  # KERNEL: sub_run_a(): ping at time                12000
  # KERNEL: sub_run_a(): ping at time                13000
  # KERNEL: sub_run_a(): ping at time                14000
  # KERNEL: sub_run_a(): ping at time                15000
  # KERNEL: sub_run_a(): ping at time                16000
  # KERNEL: sub_run_a(): ping at time                17000
  # KERNEL: sub_run_a(): ping at time                18000
  # KERNEL: sub_run_a(): ping at time                19000
  # KERNEL: sub_run_a(): ping at time                20000
  # KERNEL: sub_run_a(): ping at time                21000
  # KERNEL: sub_run_a(): ping at time                22000
  # KERNEL: sub_run_a(): ping at time                23000
  # KERNEL: sub_run_a(): ping at time                24000
  # KERNEL: sub_run_a(): ping at time                25000
  # KERNEL: sub_run_a(): ping at time                26000
  # KERNEL: sub_run_a(): ping at time                27000
  # KERNEL: sub_run_a(): ping at time                28000
  # KERNEL: sub_run_a(): ping at time                29000
  # KERNEL: sub_run_a(): ping at time                30000
  # KERNEL: sub_run_a(): ping at time                31000
  # KERNEL: sub_run_a(): ping at time                32000
  # KERNEL: sub_run_a(): ping at time                33000
  # KERNEL: sub_run_a(): ping at time                34000
  # KERNEL: sub_run_a(): ping at time                35000
  # KERNEL: sub_run_a(): ping at time                36000
  # KERNEL: sub_run_a(): ping at time                37000
  # KERNEL: sub_run_a(): ping at time                38000
  # KERNEL: sub_run_a(): ping at time                39000
  # KERNEL: sub_run_a(): ping at time                40000
  # KERNEL: sub_run_a(): ping at time                41000
  # KERNEL: sub_run_a(): ping at time                42000
  # KERNEL: sub_run_a(): ping at time                43000
  # KERNEL: sub_run_a(): ping at time                44000
  # KERNEL: sub_run_a(): ping at time                45000
  # KERNEL: sub_run_a(): ping at time                46000
  # KERNEL: sub_run_a(): ping at time                47000
  # KERNEL: sub_run_a(): ping at time                48000
  # KERNEL: sub_run_a(): ping at time                49000
  # KERNEL: sub_run_a(): ping at time                50000 
  </code></pre>