Sunday, 18 March 2018

Cleaner way to kill a fork/join thread

A cleaner way to kill any fork/join, fork/join_any, fork/join_none process would be to use SystemVerilog process construct as given below,



process threadA;
// Start a Process
fork
begin: A
 // Get process and store it to a process handle to clean up later
 threadA = process::self();
 taskA();
 taskB();
end
join_none
#0; // Let the process start

wait();
threadA.kill(); // Kill processes started by above fork cleanly

- Store process information in a process variable using process::self().
- Start parallel tasks to be run under this process.
- Use #0 delay for fork/join_none thread to get started.
- Wait for required status flag.
- End the process (here processes started by fork/join_none) with kill() method in process.


There are other ways to kill threads with "disable fork" and disable <PROC_LABEL>. Given approach is frequently used by UVM users and this is the way it has been done in UVM base class library to cleanly end processes.