Fork_join

In reply to nimitz_class:

I was asked this question in an interview and I’m not understanding why the first piece of code prints ‘4’ four times and the second one prints “0,1,2,3” when automatic is used. Someone, please explain it to me. Thanks
code1:

module test1;
int k;
initial begin
for(int i = 0; i<4; i++)begin
fork
k=i;
$display(k);
join_none
end
end
endmodule

Output:
4
4
4
4
Code 2:

module test1;
initial begin
for(int = 0; i<4 ; i++)begin
fork
automatic int k = i;
$display(k);
join_none
end
end
endmodule

Output:
0
1
2
3

In the above code 2, if I were to write the fork-join_none in this way, it wouldn’t work.
fork
begin
automatic int k = i;
$display(k);
end
join_none

Why is that? Why does the assignment of k have to be a separate parallel thread to the display function call?