For loop inside fork join_none

In reply to pawan:

Because each procedural statement inside a fork/join_none becomes a process, and that process does not start until after all the for-loop iterations. So there are 32 pending calls to thread(i) and only one instance of the variable i. And that variable will have the value 32 after the for-loop completes. So 32 is the argument that gets passed down to all 32 calls to thread3().

Now assume j is an automatic variable because this code is inside a class, or explicity declared with the prefix automatic keyword. Automatic variables get created and initialized each time the scope they are declared in gets activated. A fork/join activates a scope just like a begin/end block. A declaration is not a procedural statement (even thought the initialization may look like one). It does not become a pending process of a fork.

The code below is slightly modified, but functionally equivalent to the previous example

for(int I=0;i<32;i++)
  begin
     automatic int j = i; // automatic not needed if this code is inside a class
     fork
        begin
          thread3(j);
        end
     join_none
   end

The declaration of j is now outside the fork/join_none block, but still inside the scope of the for-loop statement. It doesn’t matter. There still will be one instance of j per iteration of the for-loop, and each initialization value of j will get the current value of i for that iteration.