Using fork join_none to span threads in a for loop in parallel, which is executed in forever loop

Hi, I have a scenario in scoreboard to always monitor activity on an interface that is parameterized. I have code something like below. But This will give me tool run out of memory issue! Can someone help with a solution to this issue?

task example_monitor_signal;
forever begin
   for (int i = 0; i < MAX_EX_PARAM; i++) begin
     automatic int j = i;
     fork
      monitor_signal (j);
     join_none
   end
end
endtask

task main_phase ();
 fork
  example_monitor_signal ();
 join_none
endtask

In reply to VarshaAnand2402:

Your code is creating infinite number of threads… and each thread is trying to run
monitor_signal.

If you look at the logic, the for_loop runs MAX_EX_PARAM times and creates monitor_signal threads. After this it exits the for_loop, jumps into the forever_loop and reenters for_loop
This creates another MAX_EX_PARAM threads all running monitor_signal.

In reply to logie:

Ok, then can you suggest how can I go about fixing this? I want to monitor the signal whenever they change and for all MAX_EX_PARAM values.

In reply to VarshaAnand2402:

can you retry after removing forever begin…end loop?

I tried this example:

class sc;
  int abc;
  parameter MAX_EX_PARAM=10;
task example_monitor_signal;
   for (int i = 0; i < MAX_EX_PARAM; i++) begin
     automatic int j = i;
     fork
      monitor_signal (j);
     join_none
   end
endtask
  
  task monitor_signal(input int a);
    wait(abc==a);
    $display("a=%d",a);
  endtask

task main_phase ();
 fork
  example_monitor_signal ();
 join_none
endtask
endclass

module abc;
  sc s1;
  
  initial begin
    s1=new();
    s1.main_phase();
    s1.abc=5;
    #2;
    s1.abc=9;
    #3;
    s1.abc=6;
  end
endmodule

output:-

vsim -voptargs=+acc=npr

run -all

a= 5

a= 9

a= 6

exit

In reply to juhi_p:

Hi Juhi,
Thanks for trying out an example. I tried this too. But it does not work everytime a value is changed. Can you explain how your example logic works everytime you change value of abc?
Regards,
Varsha

In reply to juhi_p:

Hi Juhi,
Referring to your example, If I again send same value of 5 to abc, it does not display again. My requirememt is it should monitor continously. The example you tried to help me definitely works good for one iteration.

module abc;
  sc s1;
  
  initial begin
    s1=new();
    s1.main_phase();
    s1.abc=5;
    #2;
    s1.abc=9;
    #3;
    s1.abc=6;
    #4;
    s1.abc=5;
  end
endmodule

In reply to VarshaAnand2402:

yes, that’s because it had only one thread waiting for value 5.will think through it.

In reply to VarshaAnand2402:

class sc;
  int abc;
  parameter MAX_EX_PARAM=10;
task example_monitor_signal;
   for (int i = 0; i < MAX_EX_PARAM; i++) begin
     automatic int j = i;
     fork
      monitor_signal (j);
     join_none
   end
endtask
  
  task monitor_signal(input int a);
    forever begin
    wait(abc==a);
    $display("a=%d",a);
      wait(abc!=a);
    end
  endtask

task main_phase ();
 
    fork
       example_monitor_signal ();
    join_none
    
endtask
endclass

module abc;
  sc s1;
  
  initial begin
    s1=new();
    s1.main_phase();
    s1.abc=5;
    #2;
    s1.abc=9;
    #3;
    s1.abc=6;
    #2;
    s1.abc=5;
    #6;
    s1.abc=6;
    #100;
  end
endmodule

output:-

vsim -voptargs=+acc=npr

run -all

a= 5

a= 9

a= 6

a= 5

a= 6

exit

In reply to juhi_p:

Thank you! I want to understand why is the below statement added? i tried without it and it goes to infinite loop. I am not understanding how this statemnt killed the current thread.

wait(abc!=a);

In reply to VarshaAnand2402:

thread is not killed. It will go to infinite value if you remove it, because abc=a will be immediately executes and it will run forever.
by adding abc!=a, I am letting this thread wait till changed and get the same value again.

In reply to juhi_p:

ok makes sense. Thanks a lot!!