SystemVerilog: unexpected behavior while dealing with fork/join_none

In reply to rubendah:

The forked task doesn’t execute until the parent task blocks. By the time that this happens, mbx_data has changed and this is what is passed to the task. You want to use an automatic variable:


virtual task mbx_monitor();
 
    mbx_data_object mbx_data;
 
    forever begin
        my_mbx.get(mbx_data);
        if (mbx_data.decrement_counter) begin
          automatic int id = mbx_data.id;
          fork
            wait_and_decrement_counter(id);
          join_none 
        end // if (mbx_data.decrement_counter) 
    end // forever
endtask : mbx_monitor
 
virtual task wait_and_decrement_counter(int id);
    `uvm_info(get_name(), $sformatf("Decrementing counter due to item #%0d", id), verbosity)
    #50;
    counter--;
endtask : wait_and_decrement_counter