System Verilog Threads

Q. In this I need to disable the fork as soon as the execution of any two threads completed?

module tb;

initial
begin

  fork : A
    
    begin:Thread1
      $display("Inside Thread 1");
    end:Thread1
    
    begin:Thread2
      $display("Inside Thread 2");
    end:Thread2
    
    begin:Thread3
      $display("Inside Thread 3");
    end:Thread3
    
  join_none : A
  
end

endmodule

You can use the process inbuild class in the system verilog to monitor each process . Using the process the the code will changed like this . please note as per your code the every thread will finish same time i am writing the code will take care if your thread time will change aslo .
// or browse Examples
module tb;

process p1,p2,p3;

initial
begin

fork : A

begin:Thread1
  p1=process::self();
  #1;
  $display("Inside Thread 1");
end:Thread1

begin:Thread2
  p2=process::self();
  #5;
  $display("Inside Thread 2");
end:Thread2

begin:Thread3
  p3=process::self();
  #3;
  $display("Inside Thread 3");
end:Thread3

join_none : A

wait (p1!=null && p2!=null && p3!=null) // waiting for the three process gets the value

   p1.await ;
   p3.await ;

   $display("time of after two process ends",$time);

 disable fork;

end
endmodule

if you modifed to this after any two process the disable will work

module tb;
  
  process p1,p2,p3;

initial
begin

  fork : A
    
    begin:Thread1
      p1=process::self();
      #8;
      $display("Inside Thread 1");
    end:Thread1
    
    begin:Thread2
      p2=process::self();
      #12;
      $display("Inside Thread 2");
    end:Thread2
    
    begin:Thread3
      p3=process::self();
      #3;
      $display("Inside Thread 3");
    end:Thread3
    
  join_none : A
  
  wait (p1!=null && p2!=null && p3!=null) // waiting for the three process gets the value
   
     
     fork 
       begin
         p1.await ;
         p2.await;
       end
        begin
         p1.await ;
         p3.await;
       end
        begin
         p2.await ;
         p3.await;
       end
       
     join_any
       
  
       $display("time of after two process ends",$time);
  
     disable fork;
end
endmodule

Interesting solution, but what happens if you wanted to add one or a few more processes? The amount of code you would have add becomes unmanageable. Using a semaphore is straightforward.

Then compare to semaphore number of lines of code this is much more simpler right ?
module tb;

int count ;

initial
begin

fork : A

begin:Thread1
  #8;
  $display("Inside Thread 1");
  count = count+1;
end:Thread1

begin:Thread2
  #12;
  $display("Inside Thread 2");
  count=count+1;
end:Thread2

begin:Thread3
  #3;
  $display("Inside Thread 3");
  count=count+1;
end:Thread3

join_none : A

wait (count==2);

   $display("time of after two process ends",$time);

 disable fork;

end
endmodule


module tb;
  
  semaphore sem = new();

initial
begin

  fork : A
    
    begin:Thread1
      #1;
      $display("Inside Thread 1");
      sem.put(1);
    end:Thread1
    
    begin:Thread2
      #2;
      $display("Inside Thread 2");
      sema.put(1);
    end:Thread2
    
    begin:Thread3
      #3;
      $display("Inside Thread 3");
      sema.put(1);
    end:Thread3
    
  join_none : A
  sema.get(3);
  disable fork;
  
end
endmodule

I am getting error in this code. can you please check once? @dave_59

You have a typo. “sem” vs “sema”