The code use process class to kill the thread.
class base;
int id;
task display();
int x=0;
for(int i=0;i<5;i++) begin
#10ns;
x++;
$display("%t BASE:%0d X:%0d",$time,id,x);
end
endtask
endclass
class test;
process p[4];
task run_phase();
for(int i=0;i<4;i++) begin //{
base b = new;
fork: fork_p
automatic base b1 = b;
automatic int j = i;
begin: run_d
p[j] = process::self();
b1.id = j;
b1.display();
end
join_none
end //}
$display("%t Threads spawned",$time);
endtask
endclass
module top;
test t;
initial begin
t = new;
fork
t.run_phase();
join_none
#30ns;
foreach(t.p[i]) t.p[i].kill();
end
endmodule
Result:
Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Sep 17 10:31 2024
0 Threads spawned
10 BASE:0 X:1
10 BASE:1 X:1
10 BASE:2 X:1
10 BASE:3 X:1
20 BASE:0 X:2
20 BASE:1 X:2
20 BASE:2 X:2
20 BASE:3 X:2
V C S S i m u l a t i o n R e p o r t
Time: 30 ns
CPU Time: 0.370 seconds; Data structure size: 0.0Mb
Tue Sep 17 10:31:42 2024