I need help with the code below. What is wrong in the code below?
task test_code();
int i=15;
do begin
fork
begin
#5;
$display("%t: val=%d", $time(), i);
i--;
end
begin
if(i>0) begin
#5;
$display("%t: val=%d", $time(), i);
end
end
join_none
end while (i>0);
endtask
You will need to provide more context regarding your issue. Does the code not compile? Does it not function as you expect it to? What do you expect the results to be?
One significant issue is that you have an infinite loop. A join_none statement is non-blocking, hence your do loop will run forever since i is never decremented.
Thanks cgles for fixing the code format.
The code doesn’t compile. I’m trying to run two threads where thread#1 is decrementing the value of i which is initialized to 15 and thread#2 is running till i is >0.
task test_code();
automatic int i=15;
fork
while (i>0) begin
#5;
$display("%t: val=%d", $time(), i);
i--;
end
while (i>0) begin
#5;
$display("%t: val=%d", $time(), i);
end
join
endtask
module top();
initial begin
test_code();
end
endmodule
I’m not sure what you mean as the join_none has no effect on the threads running independently of each other.
If you want to keep the second thread running forever, you can use:
task test_code();
automatic int i=15;
fork
while (i>0) begin
#5;
$display("Decrement thread: %t: val=%d", $time(), i);
i--;
end
forever begin
#5;
$display("Monitor thread: %t: val=%d", $time(), i);
end
join_none
endtask
module top();
initial begin
test_code();
#500;
$finish;
end
endmodule