# How to fork multiple forever blocks with for loop

**URL:** <https://verificationacademy.com/forums/t/how-to-fork-multiple-forever-blocks-with-for-loop/36639>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, fork-join, fork-join-in-loop, forever-loopfork-join\
**Created:** [October 7, 2019, 11:09pm UTC](https://verificationacademy.com/forums/t/how-to-fork-multiple-forever-blocks-with-for-loop/36639 "2019-10-07T23:09:19Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![NaveenReddy](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/ea5d25/32.png) [@NaveenReddy](https://verificationacademy.com/forums/u/NaveenReddy)\
**Post date:** [October 8, 2019, 6:07am UTC](https://verificationacademy.com/forums/t/how-to-fork-multiple-forever-blocks-with-for-loop/36639/7 "2019-10-08T06:07:01Z")

</div>

_In reply to [NaveenReddy](https://verificationacademy.com/forums/t/how-to-fork-multiple-forever-blocks-with-for-loop/36639/6):_

> _In reply to [dave\_59](https://verificationacademy.com/forums/t/how-to-fork-multiple-forever-blocks-with-for-loop/36639/6):_  
> Thanks Dave for the suggestion.  
> will the fork waits for all the three forever loops to join?  
> Also, in some post I read you suggesting to use it like below #1  
> what would be the best way to use #1 or #2 ?

```verilog

// #1
forever @(posedge m_vif.uclk iff xyz[i] == 1) begin
//code
end
//#2 
forever begin
@(posedge m_vif.uclk iff xyz[i] == 1);
//code
// Question: is this way #2 correct or should the code be embedded in begin end blocks ?
end

```

Dave pardon me for bugging more on this thread.  
if I want the iff condition to be applied to all the below lines  
will the #2 method i wrote below works?

```verilog

//#1 is straight forward since the code will be embedded in begin end blocks
//#2 but in this, the iff condition is terminated by ; and the following statements are not
// enclosed in begin end blocks. so, in that case, how would the code behave?
forever begin
  @(posedge m_vif.uclk iff xyz[i] == 1); //no begin end block here. so i believe this would only be applied to the immediate if condition here. Please correct me if I'm wrong
    if(!Flag[i]) begin
      wait_time[i] = getWaitTime();
      Flag[i] = 1;
    end
    memclk_cycle[i] += 1;
    if (memclk_cycle[i] == wait_time[i]) begin
      Flag[i] = 0;	
      memclk_cycle[i] = 0;
      xyz[i] = 0;
    end
end

```

---

_[View the full topic](https://verificationacademy.com/forums/t/how-to-fork-multiple-forever-blocks-with-for-loop/36639)._
