Advanced Threads (Fork-Join)
01
The automatic variable pitfall in 'fork...join_none' inside loops?
+When spawning threads inside a loop using `fork...join_none`, if the loop variable (e.g., `i`) is static (default in Verilog), all threads will see the FINAL value of `i` when they start.
Fix: Use `automatic` variables inside the loop to create a unique copy for each thread.
for (int i=0; i<3; i++) begin
automatic int k = i; // CRITICAL
fork
$display("Thread %0d", k);
join_none
end
02
`wait fork` vs `disable fork`?
+- wait fork: Waits for ALL child processes spawned by the current process to complete properly.
- disable fork: Kills ALL active children of the current process immediately.
03
How to kill a specific thread in a fork-join block?
+Assign names to the fork block, or wrap code in `begin...end` with names, and use `disable label_name`. However, usually `process` class is preferred for fine-grained control.