# SystemVerilog: unexpected behavior while dealing with fork/join\_none

**URL:** <https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738>\
**Category:** SystemVerilog\
**Tags:** fork\_joinnone, UVM, SystemVerilog, mailbox\
**Created:** [February 21, 2021, 10:18am UTC](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738 "2021-02-21T10:18:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubendah](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@rubendah](https://verificationacademy.com/forums/u/rubendah)\
**Post date:** [February 21, 2021, 10:18am UTC](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/1 "2021-02-21T10:18:21Z")

</div>

Hi everyone,

In the following code, my\_mbx is an unbounded mailbox that can be populated from multiple sources at the same simulation time:

```verilog

virtual task mbx_monitor();

    mbx_data_object mbx_data;

    forever begin

        my_mbx.get(mbx_data);

        if (mbx_data.decrement_counter) begin
 
            fork wait_and_decrement_counter(mbx_data.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

```

At time X, mailbox include two objects:  
- object #0: decrement\_counter == 1, id == 0  
- object #1: decrement\_counter == 0, id == 1

My expectation is that object #0 should call the wait\_and\_decrement\_counter task while object 1 should not, i.e. I should see the following print:

```verilog
UVM_INFO |@ X ps | uvm_test_top | Decrementing counter due to item #0

```

Actually I am seeing this one:

```verilog
UVM_INFO |@ X ps | uvm_test_top | Decrementing counter due to item #1

```

After some GUI debug, it seems that I have an issue with passing the object.id as an argument to the wait\_and\_decrement\_counter task.

Here the thread:  
- Getting item 0 from mailbox  
- Since item0.decrement\_counter == 1, I’m getting into the if condition, calling the forked task  
- Getting item 1 from mailbox  
- Since item1.decrement\_counter == 0, I’m not getting into the if condition  
- Getting into the wait\_and\_decrement\_counter task, seeing item1.id as passed argument

The trivial solution will be to add a #1 delay after the task call (i.e. into the if condition), but I would prefer to not add any delay there, can someone advise ?

Thanks in advance,  
Ruben

---

<div class="post-metadata">

**Author:** ![mike\_wang](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/mike_wang/32/1839_2.png) [@mike\_wang](https://verificationacademy.com/forums/u/mike_wang)\
**Post date:** [February 21, 2021, 12:04pm UTC](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/2 "2021-02-21T12:04:09Z")

</div>

Hi，Ruben

I think the fork join none will create a thread to carry the wait\_and\_decrement\_counter if the mbx\_data.decrement\_counter = 1；  
but it factly will carry this wait\_and\_decrement\_counter task after the main thread counter a function or task，so the first wait\_and\_decrement\_counter task will carried after the second my\_mbx.get(mbx\_data) function， so the mbx\_data will be updated , i suggest you add a `uvm\_info after the “none” key word

by the way, i want to know how long can get a full access account?  
i am a student.  
thanks so much

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [February 21, 2021, 12:21pm UTC](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/3 "2021-02-21T12:21:28Z")

</div>

_In reply to [rubendah](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/2):_

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:

```verilog

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

```

---

<div class="post-metadata">

**Author:** ![rubendah](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@rubendah](https://verificationacademy.com/forums/u/rubendah)\
**Post date:** [February 21, 2021, 12:58pm UTC](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/4 "2021-02-21T12:58:18Z")

</div>

_In reply to [mike\_wang](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/3):_

Hi Mike,

I guess I got full access since I’ve registered myself with my work mail address.

---

<div class="post-metadata">

**Author:** ![rubendah](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@rubendah](https://verificationacademy.com/forums/u/rubendah)\
**Post date:** [February 21, 2021, 12:58pm UTC](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/5 "2021-02-21T12:58:48Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/4):_

Indeed, it solved my issue, thanks !

---

<div class="post-metadata">

**Author:** ![mike\_wang](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/mike_wang/32/1839_2.png) [@mike\_wang](https://verificationacademy.com/forums/u/mike_wang)\
**Post date:** [February 21, 2021, 1:17pm UTC](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/6 "2021-02-21T13:17:23Z")

</div>

_In reply to [rubendah](https://verificationacademy.com/forums/t/systemverilog-unexpected-behavior-while-dealing-with-fork-join-none/38738/5):_  
thanks so much
