Working of fork join_none

In reply to smukerji:

Two problems with your code. First is that it create 10 parallel threads. Each statement inside a fork becomes a thread. You have two statements in a loop that iterated 5 times, so 10 threads. You probably meant to surround those two statements with a begin/end block. Then the fork sees that as a single block statement.

But the second, bigger, problem is that you have one set of variables i and j shared across all the threads. Even if you fixed the first problem, SystemVerilog won’t begin execution of any fork’ed child thread until the parent thread block, or, in this case, the parent thread completes. The value of i and j will be 5 for all threads.

What you need to do is change j to be an automatic variable so there is one copy per thread.

module tb;

initial
  for(int i=0;i<5;i++) begin
     automatic int j = i; // a new j for each entry into this block
     fork
       $display("Value of j is %d at time=%d \n", j, $time); 
     join_none
  end
endmodule