Hi,
I have a problem with the following piece of code:
module automatic test;
initial begin
for (int j=0; j<3; j++) begin
fork
$write(j);
join_none
end
wait fork;
end
endmodule
I had the assumption that because the module is automatic, local variable j would also be automatic and the printed result would be 2,1,0.
But, to my surprise, the result is 3,3,3! I can’t figure out why this happens. I would appreciate clarification.
Thanks
Variables declared as part of a for-loop are always automatic.
But it does not matter whether the local variable was static or automatic; there is only once instance of j shared among all of the forked processes. By the time those $write statements execute, j has reached its terminal value, 3.
Thank you very much, Dave. I think I get it now. As I read in the link you shared in your post, “Each statement within a fork/join_none becomes as new child process and execution of the child process does not begin until the current parent thread suspends.”, which explains why all $write statements see the last value of j.
Regards