I feel that with respect to operation of fork join and fork join_any with wait fork, both will serve the same purpose. That is the operation is same.
Fork_join :
module main ;
initial
begin
#(10);
$display(" BEFORE fork time = %2d ",$time );
fork
begin
$display("current time in Thread-1 is %0t",$time);
# (20);
$display("Thread-1 time = %2d # 20 ",$time );
end
begin
$display("current time in Thread-2 is %0t",$time);
#(10);
$display("Thread-2 time = %2d # 10 ",$time );
end
begin
$display("current time in Thread-3 is %0t",$time);
$display("Thread-3 time = %2d # 5 ",$time );
end
join
$display("time = %2d Outside the main fork ",$time );
end
endmodule
wait fork program:
module main ;
initial
begin
#(10);
$display(" BEFORE fork time = %2d ",$time );
fork
begin
$display("current time in Thread-1 is %0t",$time);
# (20);
$display("Thread-1 time = %2d # 20 ",$time );
end
begin
$display("current time in Thread-2 is %0t",$time);
#(10);
$display("Thread-2 time = %2d # 10 ",$time );
end
begin
$display("current time in Thread-3 is %0t",$time);
$display("Thread-3 time = %2d # 5 ",$time );
end
join_any
$display("time = %2d Outside the main fork ",$time );
wait fork;
$display("time = %2d After the wait fork ",$time );
end
endmodule
In reply to SaphEr:
but the final thing in both the test cases is same, it will wait for all the threads to complete.
Yes, they are same. When you use “fork…join_any” and “wait_fork”, the program will wait at “wait_fork” statement until all threads in “fork…join_any” are completed.
In reply to perumallatarun:
if you have some task instead of following timestamp print, then test behavior is different.
// $display("time = %2d Outside the main fork ",$time );
checking_method(); // for instance
Basically, the timing you exit fork-join block is totally different, so I think this 2 cases are different.
Because this post owner wanted to compare “fork…join” and “fork…any” + “wait fork”.
If you put the statement at the place like that, of course they are different.
I had two blocks A & B to be executed. Block A to be executed if any one of the threads in the fork is executed. And another Block B to be executed after all the threads in the fork are executed. So, I placed Block A after fork … join_any; and Block B after fork … join_any+wait fork;
In reply to perumallatarun:
I had two blocks A & B to be executed. Block A to be executed if any one of the threads in the fork is executed. And another Block B to be executed after all the threads in the fork are executed. So, I placed Block A after fork … join_any; and Block B after fork … join_any+wait fork;
Don’t forget that Block A and Block B also are affected each other in some cases, then be careful where you place Block A and Block B after fork block.
In reply to perumallatarun:
I had two blocks A & B to be executed. Block A to be executed if any one of the threads in the fork is executed. And another Block B to be executed after all the threads in the fork are executed. So, I placed Block A after fork … join_any; and Block B after fork … join_any+wait fork;
fork
begin /*..T1. */ end
//....
begin /*..Tn. */ end
join_any
//BLOCK A
wait fork;
//BLOCK B
No, this is not good. If you put the Block B statement like that, then Block B is only executed after all threads in fork/join_any AND “Block A” are finished. It is being affected by the time to execute “Block A”.
Here is my idea:
semaphore s = new (0);
fork
begin
/*..T1. */
s.put(1)
end
begin
/*..T2. */
s.put(1)
end
begin
/*..T3. */
s.put(1)
end
join_any
fork
begin
//BLOCK A
end
begin
s.get(3);
//BLOCK B
end
join_none