Fork join

In reply to marathuteja:

If you are just looking for the difference, I added a few more lines to your code to demonstrate it. I also added comments that may help.


module test();

  
  task method(int ii);
    #1 ; //I am adding a delay to highlight the difference.
    $display("time = %0d; i = %0d", $time,ii);
  endtask
  
  task fork_1();
    fork   //The below is all a single thread. 
      for(int i= 0; i<4; i++)
        method(i);
    join_none;
  endtask

  task fork_2();
    for ( int i= 0; i<4; i++) //There are 4 parallel thread here. You will see that all values being printed are i = 4. You will i to be "automatic" to fix that.
      fork
        method(i);
      join_none;
    endtask
  
  initial begin
    $display("Calling fork_1");
    fork_1();
    #5;  // Introducing a delay to move simulation time. Remove this to see how behaviour changes.
    $display("Calling fork_2");
    fork_2();
    #1;
    $display("The end");
  end
  
endmodule

Output :

xcelium> run
Calling fork_1
time = 1; i = 0
time = 2; i = 1
time = 3; i = 2
time = 4; i = 3
Calling fork_2
The end
time = 6; i = 4
time = 6; i = 4
time = 6; i = 4
time = 6; i = 4
xmsim: *W,RNQUIE: Simulation is complete.
xcelium> exit