- Using threads in loop may be tricky some times and if it’s nested than no words.
- But As always SV has solution as “Automatic” keyword.
- Supoose I want below algorythm to be implimented with for loop.
Than at first we think of this..
for(int i =0 ; i<10 ; i++)begin
$display(“#%0t : i = %0d random_delay =%0d”,$time,i,random_delay);
NOTE : You should never use fork-join_none directly you should write begin – end in it to avoid conflict.
- Above code will be compile free but don’t give expected output. Check it !! Problem is it will take last value of i (10) in every iteration.
- instead you should write this..
for(int i =0;i<10;i++)begin
automatic int i1; // To ensure every thread have diffrent value of i and j
$display(“#%0t : i = %0d ,random_delay =%0d”,$time,i1,random_delay);
A full version of this code is given below as EDA Playground link..Check it out..
LINK ->> EDA PLAYGROUND
Hope it helps..Keep Sharing Keep Exploring !!