A Subtle Gotcha When Using fork…join

I want to start things out light with a recent experience I've had using fork...join statements.

My intention was to start several parallel threads inside a for loop and have them take the loop parameter as an input. I naively assumed the following code would do the job:

module top;

initial begin
for (int i = 0; i < 3; i++)
fork
some_task(i);
join_none
end

task some_task(int i);
$display("i = %d", i);
endtask

endmodule

Do you see the problem with this code? If not, don't worry as I didn't at first either. Here is the output if you run it:

# i =           3
# i = 3
# i = 3

It seems that the for loop executed completely and only then did the spawned processes start, but they were given the latest value of i.

After digging around on the Internet I discovered the answer. The SystemVerilog LRM mentions that "spawned processes do not start executing until the parent thread executes a blocking statement". This explains why the for loop finished executing and why by the time the processes were spawned i had already reached the value '3'.

The LRM also shows exactly how to fix our problem: "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". Applying this to our code yields:

module top;

initial begin
for (int i = 0; i < 3; i++)
fork
automatic int j = i;
some_task(j);
join_none
end

task some_task(int i);
$display("i = %d", i);
endtask

endmodule

Now, for every loop iteration a new variable is allocated, which is then passed to the respective task. Running this example does indeed give the desired result:

# i =           2
# i = 1
# i = 0

I've been playing with parallel processes for quite some time now, but I didn't know about this until recently. I've mostly learned SystemVerilog from online tutorials, but I see that a quick read of the LRM might be of real use. Who knows what other tricks are in there?

I'll keep you updated with any more subtleties I find.

Comments