fork
begin // thread-1
$display("First Block!\n");
# 20ns;
end
begin // thread-2
$display("Second Block!\n");
@eventA;
end
join
fork...join_any
和fork_join_none
继续执行后,其一些未完成的子程序仍将在后台运行wait fork
或者disable fork
task mt_test;
fork
exce1();
exce2();
join_any
fork
exec3();
exec4();
join_none
wait fork; // block until exec1 ... exec4 complete
endtask
module tb;
task automatic exec(int id, int t);
$display("@%t exec[%0d entered]", $time, id);
#t;
$display("@%t exec[%0d exited]", $time, id);
endtask
initial begin exec(1, 10); end
initial begin exec(2, 20); end
initial begin exec(3, 30); end
initial begin exec(4, 40); end
endmodule
module tb2;
task automatic exec(int id, int t);
$display("@%t exec[%0d entered]", $time, id);
#t;
$display("@%t exec[%0d exited]", $time, id);
endtask
initial begin
exec(1, 10);
exec(2, 20);
exec(3, 30);
exec(4, 40);
end
endmodule
module tb3;
task automatic exec(int id, int t);
$display("@%t exec[%0d entered]", $time, id);
#t;
$display("@%t exec[%0d exited]", $time, id);
endtask
initial begin
fork
exec(1, 10);
exec(2, 20);
exec(3, 30);
exec(4, 40);
join
end
endmodule
module tb4;
task automatic exec(int id, int t);
$display("@%t exec[%0d entered]", $time, id);
#t;
$display("@%t exec[%0d exited]", $time, id);
endtask
initial begin: ini_proc1
$display("@%t fork-join_any entered", $time);
fork
exec(1, 10);
exec(2, 20);
join_any
$display("@%t fork-join_any exited", $time);
$display("@%t fork-join_none entered", $time);
fork
exec(3, 30);
exec(4, 40);
join_none
$display("@%t fork-join_none exited", $time);
$display("@%t ini_proc1 exited", $time);
end
initial begin
#200;
end
endmodule
initial本该在10ns的时候退出,但是fork块内的线程还没有结束
wait fork
可以让当前initial块等待前面的fork
执行完后再继续
module tb5;
task automatic exec(int id, int t);
$display("@%t exec[%0d entered]", $time, id);
#t;
$display("@%t exec[%0d exited]", $time, id);
endtask
initial begin: ini_proc1
$display("@%t fork-join_any entered", $time);
fork
exec(1, 10);
exec(2, 20);
join_any
$display("@%t fork-join_any exited", $time);
$display("@%t fork-join_none entered", $time);
fork
exec(3, 30);
exec(4, 40);
join_none
$display("@%t fork-join_none exited", $time);
wait fork;
$display("@%t ini_proc1 exited", $time);
end
initial begin
#200;
end
endmodule
disable fork
可以让当前initial内没有执行完的fork线程结束
module tb6;
task automatic exec(int id, int t);
$display("@%t exec[%0d entered]", $time, id);
#t;
$display("@%t exec[%0d exited]", $time, id);
endtask
initial begin: ini_proc1
$display("@%t fork-join_any entered", $time);
fork
exec(1, 10);
exec(2, 20);
join_any
$display("@%t fork-join_any exited", $time);
$display("@%t fork-join_none entered", $time);
fork
exec(3, 30);
exec(4, 40);
join_none
$display("@%t fork-join_none exited", $time);
disable fork;
$display("@%t ini_proc1 exited", $time);
end
initial begin
#200;
end
endmodule
#
来完成@
来完成wait
语句也可以与事件或者表达式结合来完成