Fork join_none when is scope entered

module my_program;

  int unsigned num = 3;
  int unsigned max_delay = 3;

  initial begin
    for (int i = 0; i < num; i++) begin
       $display("%t : For loop entered . Iteration %0d", $time, i);
       fork
         automatic int local_i = i;
         $display("%t : Assigned local variable local_i = %0d", $time, local_i);
         begin
           automatic int j = local_i;
           $display("%t child begin-end process %d is started and will do #%0d delay",
           $time, local_i, j);
           #j;
           $display("%t child begin-end process %0d finished",
                    $time, local_i);
         end // begin
       join_none
    end // end for loop
    $display("%t : For loop completed, now starting wait of %0d time units", $time, (num + 1));
    #(num + 1);
    disable fork;
  end // initial
endmodule

It is not clear to me what LRM specifies "Automatic variables declared in the scope of the fork?join block shall be initialized to the initialization value whenever execution enters their scope, and before any processes are spawned."

Q2: When was the execution scope of the fork join block actually entered and when is the begin..end scope entered?

Q3: Are

automatic int local_i = i;

and

begin ... end

considered parallel threads inside the fork join_none? Then how do we guarantee that assignment happens before begin .. end

 

 

In reply to verifrus:

Please use code tags making your code easier to read, especially indenting of begin and fork blocks. I have added them for you.

Think of begin/end and fork/join as a single statement that happen to enclose one or more statements.
Your initial statement starts a process with one statement. That statement is a begin/end block with 4 enclosing statements (for, $display, #(num+1), and disable).

The for loop statement is begin/end block with 2 enclosing statements ($display, and fork/join_none). The $display followed by the fork statements get executed

The fork/join_none encloses an automatic variable declaration/initialization and 2 statements ($display and begin/end) The automatic variable initialization happens as soon as the fork statement get executed, but the 2 enclosing statements get queued in the background for execution and don’t start until the process gets to the #(num+1) statement, which at that point there will be 6 processes queued up. As you quoted from the LRM, the automatic variables are already initialized before any processes start. And the semantics of a fork/join_none are that those processes do not start until the parent process blocks.

P.S. I moved the declarations of num and max_delay outside of the initial/begin end block outside the block because that was illegal the way you had originally wrote them. See function arguments not initializing variable inside the body | Verification Academy