task delay_print(int wait_time);
#wait_time $display("printing after %d\n", wait_time);
endtask
module test;
initial begin
fork
delay_print(5);
delay_print(10);
join_none
end
endmodule
If I execute the above code, notice that there is no automatic in task “delay_print”. So there is only one memory stack. But how come I get “printing after 10” twice. Intuitively thinking, shouldn’t I get “printing after 5” twice because delayed 5 occurs before delayed 10 and jump out of fork join loop first? Can someone explain how it works? Thanks!
Although the child threads within fork join / join_any / join_none execute in parallel, tools seem to execute it in source order i.e delay_print(5) would be called before delay_print(10)
I believe LRM doesn’t clarify the order of executing the child threads as well i.e execution order is non-deterministic
(1) The 1st call to delay_print with input argument as 5 blocks due to #5
(2) The 2nd call to delay_print with input argument as 10 blocks due to #10
Since the task ‘delay_print’ is static the latter calls to the same task override the input argument ‘wait_time’ to the last actual argument i.e 10
If you re-write the task as
task delay_print(int wait_time);
#wait_time $display("T:%0t printing after %d\n",$realtime,wait_time);
endtask
You would observe that the 1st display executes at 5 time units and 2nd display at 10 time units