ch4-Fork join

2019-10-24

三种fork

What are different fork - join styles ?

We have three different styles of fork join in SystemVerilog.

fork-join

fork join Finishes when all child threads are over
fork join_any Finishes when any child thread gets over
fork join_none Finishes soon after child threads are spawned

disable fork

What happens when fork is disabled ?

The same example is taken from above, and disable fork is added towards the end.

Note that Thread2 and Thread3 got killed because of disable fork

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module tb_top;

initial begin
// Fork off 3 sub-threads in parallel and the currently executing main thread
// will finish when any of the 3 sub-threads have finished.
fork

// Thread1 : Will finish first at time 40ns
#40 $display ("[%0t ns] Show #40 $display statement", $time);

// Thread2 : Will finish at time 70ns
begin
#20 $display ("[%0t ns] Show #20 $display statement", $time);
#50 $display ("[%0t ns] Show #50 $display statement", $time);
end

// Thread3 : Will finish at time 60ns
#60 $display ("[%0t ns] TIMEOUT", $time);
join_any

// Display as soon as the fork is done
$display ("[%0tns] Fork join is done, let's disable fork", $time);

disable fork;
end
endmodule

Simulation Log

1
2
3
4
5
6
ncsim> run
[20 ns] Show #20 $display statement
[40 ns] Show #40 $display statement
[40ns] Fork join is done, let's disable fork
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit

wait fork

wait fork allows the main process to wait until all forked processes are over. This is useful in cases where the main process has to spawn multiple threads, and perform some function before waiting for all threads to finish.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module tb_top;

initial begin
// Fork off 3 sub-threads in parallel and the currently executing main thread
// will finish when any of the 3 sub-threads have finished.
fork

// Thread1 : Will finish first at time 40ns
#40 $display ("[%0t ns] Show #40 $display statement", $time);

// Thread2 : Will finish at time 70ns
begin
#20 $display ("[%0t ns] Show #20 $display statement", $time);

#50 $display ("[%0t ns] Show #50 $display statement", $time);
end

// Thread3 : Will finish at time 60ns
#60 $display ("[%0t ns] TIMEOUT", $time);
join_any

// Display as soon as the fork is done
$display ("[%0t ns] Fork join is done, wait fork to end", $time);

// Fork two more processes
fork
#10 $display ("[%0t ns] Wait for 10", $time);
#20 $display ("[%0t ns] Wait for 20", $time);
join_any

// Wait until ALL forked processes are over
wait fork;
$display ("[%0t ns] Fork join is over", $time);
end
endmodule

Simulation Log

1
2
3
4
5
6
7
8
9
10
ncsim> run
[20 ns] Show #20 $display statement
[40 ns] Show #40 $display statement
[40 ns] Fork join is done, wait fork to end
[50 ns] Wait for 10
[60 ns] TIMEOUT
[60 ns] Wait for 20
[70 ns] Show #50 $display statement
[70 ns] Fork join is over
ncsim: *W,RNQUIE: Simulation is complete.

Click to try this example in a simulator! img


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!