Hi @dave_59,
In below code, why object has deallocated when assigned with special value null
when threads spawned by fork-join_none
are still running?
Could you please tell if the following statements are true as per SystemVerilog:
- SystemVerilog can’t garbage collects an object that is still referenced by a handle.
- An object can’t be deallocated that is used by a spawned thread and threads are still running. - Applicable in my case.
- Setting a handle to special value
null
doesn’t guarantee immediate deallocation because the garbage collection process will still check for any active references before an object deallocation.
Code:
program automatic top;
class ObjectDeallocation;
int a;
function new();
a = 10;
endfunction
endclass
task insert_wait(ObjectDeallocation obj);
fork
#10 $display("%0t: THREAD1 Completed", $time);
#20 $display("%0t: THREAD2 Completed", $time);
join_none
$display("%0t: TASK Completed", $time);
endtask
initial begin
ObjectDeallocation obj;
obj = new();
fork
insert_wait(obj);
#1 insert_wait(obj);
join_none
obj = null;
$display("%0t: After object deallocation", $time);
end
endprogram
Expected Output: I am expecting either Output_1 or Output_2
Output_1:
0: TASK DONE (From 1st thread spwaned by fork-join_none in begin-end)
1: TASK DONE (From 2nd thread spwaned by fork-join_none in begin-end)
10: THREAD1 Completed (from 1st thread ...)
11: THREAD1 Completed (from 2nd thread ...)
20: THREAD2 Completed (from 1st thread ...)
21: THREAD2 Completed (from 2nd thread ...)
(once all threads are completed/terminated, object deallocation happens now)
21: After object deallocation
Output_2:
Simulation should give an error when it encounters object deallocation statement i.e. obj = null; because threads are still running and code is trying to deallocate an object forcefully while still active references for an object are present.
Actual Output:
0: After object deallocation
Thanks,
Naveen Kadian