# Disable fork join when one of the tasks complete

**URL:** <https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758>\
**Category:** SystemVerilog\
**Tags:** fork-join\_any, SystemVerilog, fork-join-in-loop\
**Created:** [January 18, 2017, 3:41pm UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758 "2017-01-18T15:41:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [January 18, 2017, 3:41pm UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/1 "2017-01-18T15:41:46Z")

</div>

I have the following code in the run\_phase of the monitor:

```verilog
virtual task run_phase (uvm_phase phase);
      forever begin
                fork 
                    monitor_cycle_count();
                    forever begin
                       mon_trx = axi_transaction::type_id::create("mon_trx");
                       fork
                                  monitor_write();
                                  monitor_read();                 
                       join_any
                    end
                join
      end
  endtask: run_phase

```

I use fork\_any because I want that when one of the tasks completes (monitor\_write/monitor\_read), a new loop will start with new transaction.  
But I can see that even when for example tasks which the monitor\_write()contains continue to execute even when monitor\_write() is done, with the same transaction.

---

<div class="post-metadata">

**Author:** ![R.B\_Vijey\_Shankar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@R.B\_Vijey\_Shankar](https://verificationacademy.com/forums/u/R.B_Vijey_Shankar)\
**Post date:** [January 18, 2017, 4:37pm UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/2 "2017-01-18T16:37:03Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758):_

Hi,  
You can give a name to the fork…join\_any and disable it after join\_any.

---

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [January 18, 2017, 5:00pm UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/3 "2017-01-18T17:00:03Z")

</div>

_In reply to [R.B. Vijey Shankar](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/2):_

1. How can I give name?  
2.Is there any option to disable from a task which is contained in one of the task of the fork?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 19, 2017, 12:00am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/4 "2017-01-19T00:00:35Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/3):_  
Having nested   
forever loops does not make much sense. Normally you put   
disable fork after a   
join\_any. Doing that with your example would not disable the process calling the monitor\_cycle\_count() because it is a sibling to the thread doing the disable, not a child of it.

Maybe you intended to write

```verilog
virtual task run_phase (uvm_phase phase);
forever begin
          mon_trx = axi_transaction::type_id::create("mon_trx");
          fork 
            monitor_cycle_count();
          join_none
          fork
            monitor_write();
            monitor_read();                 
          join_any;
          disable fork;
      end
  endtask: run_phase

```

The   
disable fork above will wait for one of the monitor\_read/write tasks to finish, then kill the other one as well as killing the monitor)cycle\_count task if it is still running. It was not very clear what your intention was.

---

<div class="post-metadata">

**Author:** ![kddholak](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kddholak/32/1363_2.png) [@kddholak](https://verificationacademy.com/forums/u/kddholak)\
**Post date:** [January 19, 2017, 12:53am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/5 "2017-01-19T00:53:08Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/4):_  
you should use process in your case or labeled , code is not tested .

```verilog

virtual task run_phase (uvm_phase phase);
     process thread ;
      forever begin
                fork 
                    monitor_cycle_count();
                    forever begin
                       mon_trx = axi_transaction::type_id::create("mon_trx");
                       fork
                                  thread = process::self();
                                  monitor_write();
                                  monitor_read();                 
                       join_any
                       thread.kill();
                    end
                join
      end
  endtask: run_phase

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 19, 2017, 3:57am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/6 "2017-01-19T03:57:14Z")

</div>

_In reply to [kddholak](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/5):_  
Your code does not work. The thread that you kill will already be completed.

---

<div class="post-metadata">

**Author:** ![kddholak](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kddholak/32/1363_2.png) [@kddholak](https://verificationacademy.com/forums/u/kddholak)\
**Post date:** [January 19, 2017, 6:04am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/7 "2017-01-19T06:04:52Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/6):_

Thanks Dave . I have updated the solution.

```verilog

virtual task run_phase (uvm_phase phase);
     process thread[$] ;
      forever begin
                fork 
                    monitor_cycle_count();
                    forever begin
                       mon_trx = axi_transaction::type_id::create("mon_trx");
                       fork
                                 begin
                                  thread.push_back(process::self());
                                  monitor_write();
                                  end begin
                                  thread.push_back(process::self());
                                  monitor_read();                 
                                  end 
                       join_any
                       foreach(thread[i]) thread[i].kill();
                       thread = {}; // delete the Q
                  end
                join
      end
  endtask: run_phase

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 19, 2017, 6:25am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/8 "2017-01-19T06:25:10Z")

</div>

_In reply to [kddholak](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/7):_  
What you wrote is exactly what a disable fork does. Also does not address the problem with the nested forever loops.

---

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [January 22, 2017, 9:35am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/9 "2017-01-22T09:35:39Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/8):_

The disable fork shouldn’t kill the monitor\_cycle\_count() (this task should always run, that’s why I put it in forever loop).  
The monitor\_write()and monitor\_read()tasks should run parallel on each new transaction (because I don’t know if it will be read or write transactiobn… this code is part of uvm\_monitor). That’s why I put them also in forever loop.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 23, 2017, 12:18am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/10 "2017-01-23T00:18:49Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/9):_  
This code below using   
disable fork is identical to kddholak’s most recent post above using the   
kill() method

```verilog
virtual task run_phase (uvm_phase phase);
      forever begin
                fork 
                    monitor_cycle_count();
                    forever begin
                       mon_trx = axi_transaction::type_id::create("mon_trx");
                       fork
                                 begin
                                  monitor_write();
                                  end begin
                                  monitor_read();                 
                                  end 
                       join_any
                       disable fork;
                  end
                join
      end
  endtask: run_phase

```

The problem with either of these, is that the inner   
forever loop is persistent. It never terminates, so the   
fork/join never joins, so the outer   
forever loop never repeats.

---

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [January 23, 2017, 10:19am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/11 "2017-01-23T10:19:15Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/10):_

What for the begin and end inside the fork?  
As I said this code is a part of the monitor, that’s why the forever loop.  
Shouldn’t I use forever loop?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 23, 2017, 9:31pm UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/12 "2017-01-23T21:31:22Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/11):_

> \*In reply to [dave\_59](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/11):\*What for the begin and end inside the fork?

I was just trying to keep the two examples as similar as possible. A begin/end around a single statement does not do anything meaningful. The fork/join\_any still creates two processes.

Look at this simpler example

```verilog
module top;
int A,B,C;
initial forever fork : outer
            #1 A = A + 1;
            forever begin : inner
                fork
                  #2 B = B + 1;
                  #4 C = C + 1;
                join_any
                disable fork;
             end : inner
         join : outer
endmodule

```

The outer **forever** loop only executes once; it behaves the same as if there was no outer forever keyword.

The outer fork creates two processes, one for the   
 #1 A = A + 1; statement, and one for the inner **forever** loop. The inner forever loop executes a inner fork/join\_any that creates two more processes; the assignments to B and C.  
The process that makes the assignment to A completes at time 1, then the process that makes the assignment to B completes at time 2. The join\_any happens at time 2, and the process that would make the assignment to C gets disabled. Now we have gotten to the end of the inner forever loop, and we repeat the inner fork. So the assignment to B happens at times 2,4,6,8, etc. The assignment to C never occurs because the statement gets disabled before ever having a chance to execute.  
The outer fork never joins because the second process it spawns never finishes. It is an infinite forever loop. Since the outer fork never joins, the outer forever loop never has a chance to repeat.

---

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [January 24, 2017, 9:41am UTC](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/13 "2017-01-24T09:41:18Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/disable-fork-join-when-one-of-the-tasks-complete/32758/12):_

Yes but what if the process is timing process how can I ensure that if one start (the first condition in the task existed) the other one won’t start?
