创建线程有三种方式
(1)fork…join
(2)fork…join_any
(3)fork…join_none
从图中可以看出
task run_threads;
...
fork
check_trans(tr1);
check_trans(tr2);
check_trans(tr3);
join_any
...
wait fork //等待三个check执行完毕再退出task
endtask
parameter TIME_OUT = 1000;
task check_trans(Transaction tr);
fork
begin
fork:timeout_block
begin
wait(bus.cb.addr == tr.addr)
$display("@%0t:Addr match %d",$time,tr.addr);
end
#TIME_OUT $display("@%0t:Error:timeout",$time);
join_any
//停止TIME_OUT语句
disable timeout_block;
end
join_none
endtask
(2)停止多个进程
initial begin
check_trans(tr0);
fork
begin
check_trans(tr1);
fork
check_trans(tr2);
join
//这里有两个fork,如果不指明,会将两个fork都需要停止,所以需要区分
#(TIME_OUT/2) disable fork;
end
join
end
(3)停止被多次调用的任务
task wait_for_time_out(int id);
if(id==0)
fork
begin
#2
$display("@%0t:disable wait_for_time_out",$time);
disable wait_for_time_out;
end
join_none
fork:just_a_little
begin
$diaplay("@%0t:%m:%0d entering thread",$time,id);
#TIME_OUT;
$display("@%0t:%m:%0d done",$time,id);
end
join_none
endtask
initial begin
wait_for_time_out(0);//被停止
wait_for_time_out(1);//被停止
wait_for_time_out(2);//被停止
#(TIME_OUT*2)$display("@%0t:All done",$time);
end