Issue with forks inside for loop

Hi,
consider the following code:


 fork 
  begin
    for(int i = 0; i < 10; i++)
    begin
       automatic int j = i; 
       fork
          begin
            forever
            begin
              @(evt_A[j]);
              $display("AA");
            end      
            forever
            begin
              @(evt_B[j]);
              $display("BB");
            end      
          end 
       join_none
    end
  end
join_none

Here the result I expected was:
AA
BB
(PS: I ensure that both evt_A and evt_B is triggered for some j).
But the above code did not give that outcome.
So when I rephrased the code to what I have shown below, I got the expected results. Could anyone pls explain why the first code couldn’t quite cut it right.


 fork 
  begin
    for(int i = 0; i < 10; i++)
    begin
       automatic int j = i; 
       fork
          begin
            forever
            begin
              @(evt_A[j]);
              $display("AA");
            end          
          end 
       join_none
    end
  end
  begin
    for(int i = 0; i < 10; i++)
    begin
       automatic int j = i; 
       fork
          begin
            forever
            begin
              @(evt_B[j]);
              $display("BB");
            end          
          end 
       join_none
    end
  end
join_none

Thanks in advance for the help!

In reply to nipradee:

In your first code,


       fork
          begin
            forever
            begin
              @(evt_A[j]);
              $display("AA");
            end      
            forever
            begin
              @(evt_B[j]);
              $display("BB");
            end      
          end 
       join_none

The issue is you have two forever loops in your begin and end statements. So second loop will start only after first loop is done. Likely your second loop never started. To fix this remove begin and end inside fork this will create two seperate threads for forever loops.


fork
  forever begin
  end
  forever begin
  end
join_none

The begin/end inside the fork was indeed the issue. Thanks