fork join
SystemVerilog 通过构造提供对并行或并发threads的支持。可以使用fork
和join
同时生成多个procedural blocks。有一些变体允许main thread根据child thread何时完成继续执行其余语句。
Syntax
fork
// Thread 1
// Thread 2
// ...
// Thread 3
join
fork join example
在下面显示的示例中,使用了forked的3个threads。main thread将保持暂停状态,直到forked生成的所有threads都完成。begin
和end
中的任何代码块都被视为单独的thread,在本例中为Thread 2
。
module tb;
initial begin
$display ("[%0t] Main Thread: Fork join going to start", $time);
fork
// Thread 1
#30 $display ("[%0t] Thread1 finished", $time);
// Thread 2
begin
#5 $display ("[%0t] Thread2 ...", $time);
#10 $display ("[%0t] Thread2 finished", $time);
end
// Thread 3
#20 $display ("[%0t] Thread3 finished", $time);
join
$display ("[%0t] Main Thread: Fork join has finished", $time);
end
endmodule
main thread在时间0ns时forked所有3个threads。Thread2是一个procedural blocks,只有当它执行begin
和end
中的所有语句时才会完成。Thread2需要15ns才能完成,并且由于它从0ns开始,所以它在15ns结束,并且是第一个完成的thread。Thread1在30ns时完成模拟时间最多,而Thread3在20ns时更早完成。
模拟日志
ncsim> run
[0] Main Thread: Fork join going to start
[5] Thread2 ...
[15] Thread2 finished
[20] Thread3 finished
[30] Thread1 finished
[30] Main Thread: Fork join has finished
ncsim: *W,RNQUIE: Simulation is complete.
Nested fork join
fork join
也可以嵌套在其他中。
Example #1
module tb;
initial begin
$display ("[%0t] Main Thread: Fork join going to start", $time);
fork
fork
print (20, "Thread1_0");
print (30, "Thread1_1");
join
print (10, "Thread2");
join
$display ("[%0t] Main Thread: Fork join has finished", $time);
end
// Note that this task has to be automatic
task automatic print (int _time, string t_name);
#(_time) $display ("[%0t] %s", $time, t_name);
endtask
endmodule
模拟日志
ncsim> run
[0] Main Thread: Fork join going to start
[10] Thread2
[20] Thread1_0
[30] Thread1_1
[30] Main Thread: Fork join has finished
ncsim: *W,RNQUIE: Simulation is complete.
Example #2
module tb;
initial begin
$display ("[%0t] Main Thread: Fork join going to start", $time);
fork
// Thread 1
fork
#50 $display ("[%0t] Thread1_0 ...", $time);
#70 $display ("[%0t] Thread1_1 ...", $time);
begin
#10 $display ("[%0t] Thread1_2 ...", $time);
#100 $display ("[%0t] Thread1_2 finished", $time);
end
join
// Thread 2
begin
#10 $display ("[%0t] Thread2 ...", $time);
#100 $display ("[%0t] Thread2 finished", $time);
end
// Thread 3
#20 $display ("[%0t] Thread3 finished", $time);
join
$display ("[%0t] Main Thread: Fork join has finished", $time);
end
endmodule
看到main thread保持挂起状态,直到所有嵌套的forked结束110ns。
模拟日志
ncsim> run
[0] Main Thread: Fork join going to start
[5] Thread2 ...
[10] Thread1_2 ...
[15] Thread2 finished
[20] Thread3 finished
[50] Thread1_0 ...
[70] Thread1_1 ...
[110] Thread1_2 finished
[110] Main Thread: Fork join has finished
ncsim: *W,RNQUIE: Simulation is complete.
标签:fork,join,0t,Thread,--,finished,time,display,SystemVerilog
From: https://www.cnblogs.com/sys-123456/p/18188244