Fork/join_none at funciton/task call or definition

Hi All,

Please help in understanding the below question related to fork/join_none/join/join_any

  1. When we try to use the fork/join_none during the function/task call i am getting the below output.
module test;
     initial begin 
    for(int i = 0; i < 3;i++)
    begin
      fork 
	  print(i);
      join_none
	end 
end 
task print (int i);
    $display("[%0t]: i = %0d",$time,i);
endtask:print
endmodule:test
/*
[0]: i = 3
[0]: i = 3
[0]: i = 3
*/
  1. when we try to use the fork/join_none during the function/task definition i am getting the below output.
module test;
initial begin 
  for(int i = 0; i < 3;i++)
    begin
       print(i);
     end 
end 
task print (int i);
  fork
   $display("[%0t]: i = %0d",$time,i);
  join_none
endtask:print
endmodule:test

/*
[0]: i = 2
[0]: i = 2
[0]: i = 2
]> */

  1. what is the difference between the approach 1 and 2 . when to use it ?

Please provide some inputs with some practical example.

thanks in advance

Please provide any updates on this.

In both cases you have two variables both declared i; one in the for-loop, and one inside the task test.print.i. The difference is when the value of i in the for loop gets passed to the variable inside the task. And note the variable inside the task is static, so there is only one instance of i shared by all calls to the task print.

In your first approach, fork/join_none processes do not start until after the for loop terminates, so 3 gets passed to each call to print(i).

In your second approach, the values 0, 1, and 2, get passed to print(i), but since there is only one copy of the variable i inside the task, the last write 2 wins before the the fork/join_none processes start.

That explains the behavior of your code, but without knowing what you are trying to achive, that is all I can say. You may want to look at my DVCon paper The life of a SystemVerilog variable for some ideas.

1 Like