# Resume simulation when any 2 threads out of 3 get completed within fork-join\_any

**URL:** <https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572>\
**Category:** SystemVerilog\
**Tags:** fork-join\_any, SystemVerilog\
**Created:** [November 19, 2016, 8:19am UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572 "2016-11-19T08:19:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sagar\_Shah](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/sagar_shah/32/1963_2.png) [@Sagar\_Shah](https://verificationacademy.com/forums/u/Sagar_Shah)\
**Post date:** [November 19, 2016, 8:19am UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/1 "2016-11-19T08:19:55Z")

</div>

There are three parallel threads running in fork-join\_any.  
I want to come-out from that when any two threads get completed.  
How to do this?

---

<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:** [November 19, 2016, 4:19pm UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/2 "2016-11-19T16:19:42Z")

</div>

_In reply to [Sagar Shah](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572):_  
You can use a semaphore that waits for 2 keys

```verilog
semaphore s;

s = new(0);
fork
   begin : process1
    ...
    s.put(1);
   end
 begin : process2
    ...
    s.put(1);
   end
 begin : process3
    ...
    s.put(1);
 end
join_none
s.get(2);
disable fork; // kills remaining process
s = null; // removes semaphore (if you plan to reuse s)

```

---

<div class="post-metadata">

**Author:** ![Sagar\_Shah](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/sagar_shah/32/1963_2.png) [@Sagar\_Shah](https://verificationacademy.com/forums/u/Sagar_Shah)\
**Post date:** [November 21, 2016, 5:59am UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/3 "2016-11-21T05:59:53Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/2):_

Thanks for quick solution.  
Today I came to know actual usage of semaphore.

---

<div class="post-metadata">

**Author:** ![amiya957](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/958977/32.png) [@amiya957](https://verificationacademy.com/forums/u/amiya957)\
**Post date:** [April 16, 2019, 11:31am UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/4 "2019-04-16T11:31:57Z")

</div>

Is there any solution possible without the use of semaphore?

---

<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:** [April 16, 2019, 5:00pm UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/5 "2019-04-16T17:00:51Z")

</div>

_In reply to [amiya957](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/4):_

There are other solutions like using a simple counter, but they are not considered “_thread-safe_”. They would work fine in most situations, but the LRM does not guarantee atomic execution when multiple processes are writing to the same variable. i.e. if two process simultaneously execute N=N+1, there is no guarantee that N winds up being incremented by 2.

With today’s tools, I would say this is an impossible situation, but as your code gets more complex, and as tools adopt multi-core technologies, it could happen. So it’s better to code it the _safe_ way.

---

<div class="post-metadata">

**Author:** ![Etrx91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/439d5e/32.png) [@Etrx91](https://verificationacademy.com/forums/u/Etrx91)\
**Post date:** [April 16, 2019, 6:03pm UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/6 "2019-04-16T18:03:41Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/5):_

Hi Dave ,

Could it possible using Fine-Grain Process Control ? . Particularly " await " and  
" process.status != FINISHED "

Also your suggestion regarding counters being " thread unsafe" is Certainly Crucial .  
What I am curious to know is what could be the counter be incremented to ( if not by 2 ) .  
Since its a blocking statement although there would be no atomic guarantee , wouldn’t the 2nd Incremental Statement always see the Updated Value ( i.e incremented by 1 ) ?

Not arguing , just curious to know

Thanks ,

AGIS

---

<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:** [April 16, 2019, 10:00pm UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/7 "2019-04-16T22:00:06Z")

</div>

_In reply to [Etrx91](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/6):_

Yes, there are a number of ways to accomplish the same, but all require access to a shared memory variable with some sort of process count. Incrementing a location in memory involves three steps: read, modify(increment), write. It’s a blocking assignment statement in the sense that within the same thread, these three steps must occur in order before the next statement executes. But there’s no guarantee that another process could be executing the same steps interweaved with each other.

---

<div class="post-metadata">

**Author:** ![Mahesh\_6](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/ecd19e/32.png) [@Mahesh\_6](https://verificationacademy.com/forums/u/Mahesh_6)\
**Post date:** [May 15, 2025, 4:28pm UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/8 "2025-05-15T16:28:23Z")

</div>

Hi All,

the above example by using .put will work only if process timings are different, that is  
T1 != T2 != T3;

But I have a scenario, where time intervals between all process are same,  
that is T1 = T2 = T3;  
in this case, as shown the code below, I am interested in only two process. After two process, I should exit the fork, May I know how can I achieve this?

**Currently I can see all process are getting executed, but I want any two of them only to be executed.**

Kindly suggest

```auto
module fork_join_sample;
semaphore sem = new;
  
      task t1;
        #5 $display($time,"\t Process_1");
        sem.put(1);
      endtask
    
      task t2;
        #5 $display($time,"\t Process_2");
        sem.put(1);
      endtask
    
      task t3;
        #5 $display($time,"\t Process_3");
        sem.put(1);
      endtask
    
     initial begin
       fork
         t1;
         t2;
         t3;
       join_none
       $display("OUT OF FORK");
       sem.get(2);
       disable fork;
     end
      
endmodule

-----------------------
OUTPUT:
xcelium> run
OUT OF FORK
                   5 Process_1
                   5 Process_2
                   5 Process_3

```

---

<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:** [May 15, 2025, 5:00pm UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/9 "2025-05-15T17:00:27Z")

</div>

In that case you have a race condition. All three processes terminate at the same time and the `disable fork` makes no sense.

---

<div class="post-metadata">

**Author:** ![adikp98](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c89c15/32.png) [@adikp98](https://verificationacademy.com/forums/u/adikp98)\
**Post date:** [November 12, 2025, 5:54pm UTC](https://verificationacademy.com/forums/t/resume-simulation-when-any-2-threads-out-of-3-get-completed-within-fork-join-any/32572/10 "2025-11-12T17:54:15Z")

</div>

How can we use semaphores if we are allowed to use only fork.. join ? How can we write the logic to disable?
