Use fork-join_none method in for loop

  • 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
 fork begin 
  $display(“#%0t : i = %0d  random_delay =%0d”,$time,i,random_delay);
wait fork;

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
 fork  begin 
 $display(“#%0t : i = %0d ,random_delay =%0d”,$time,i1,random_delay);
 wait fork;

A full version of this code is given below as EDA Playground link..Check it out..


Hope it helps..Keep Sharing Keep Exploring !!

One response

  1. Amiya Ranjan Dash | Reply

    This problem happens only for fork….join_none?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: