Title

SystemVerilog -- 3.2 SystemVerilog Threads --> fork join_any

fork join_any

在一个简单的SystemVerilog中,main thread会等到所有child thread都完成执行。这意味着如果任何child thread永远运行并且永远不会完成,则fork将挂起模拟。SystemVerilog还提供了带有fork joinfork join_any的原始版和变体。

如果任何一个child thread完成,则允许main thread继续执行位于fork之后的其它语句。如果启动了5 threads,则只有当5 threads中的任何一个thread完成执行时,main thread才会恢复执行。main thread恢复运行后,其余启动的4 threads将继续在后台运行。

Syntax

fork
  // Thread 1
  // Thread 2
  // ...
  // Thread N
join_any

fork join_any Example

module tb;
  initial begin
    $display ("[%0t] Main Thread: Fork join going to start", $time);
    fork
      print (20, "Thread1_0");
      print (30, "Thread1_1");
      print (10, "Thread2"); 
    join_any
    $display ("[%0t] Main Thread: Fork join has finished", $time);
  end

  // Note that this task needs 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
[10] Main Thread: Fork join has finished
[20] Thread1_0
[30] Thread1_1
ncsim: *W,RNQUIE: Simulation is complete.

Nested fork join_any

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_any
    print (10, "Thread2");
    join_any
    $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
[10] Main Thread: Fork join has finished
[20] Thread1_0
[20] Thread1_1
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit

posted on 2024-05-13 22:29  松—松  阅读(2)  评论(0编辑  收藏  举报

导航